Forum Moderators: open
<html>
<head>
<script language='javascript' src='/my_source.js'>
</script>
</head>...
How would I call on an external javascript from inside a javascript?
Any thoughts are appreciated.
[edited by: kirkplug at 12:13 am (utc) on Nov. 8, 2004]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function scrLdr(){
var scr_tg=document.getElementById('scr2');
scr_tg.src="tst_181.js";
}
</script>
</head>
<body>
<p>
<script id="scr2" type="text/javascript" src=""></script>
<a href="#" onclick="scrLdr();">Load new script</a>
<br /><br /><br />
<a href="#" onclick="ckNew();">Check new code</a>
</p>
</body>
</html>
And "tst_181.js" contains:
function ckNew(){
alert("new");
}
Alternatively, you could use a hidden iframe to load an HTML page with scripts (internal or external), then call the scripts through the parent/child relationship as with:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body onload="document.getElementById('ifrOne').src='tst_181_c.htm';">
<p>
<a href="#" onclick="top.frames[0].ckNew();">Check iframe load</a>
</p>
<iframe id="ifrOne" src="" style="visibility:hidden;"></iframe>
</body>
</html>
And "tst_181_c.htm" contains:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function ckNew(){
alert("iframe loaded");
}
</script>
</head>
<body>
</body>
</html>
Obviously, this creates unnecessary overhead. You should really plan your scripts so you can load your script files in the head of your page, using HTML.