Welcome to WebmasterWorld Guest from 50.19.34.234

Forum Moderators: open

Message Too Old, No Replies

external Javascripts WITHIN a javascript

     

kirkplug

12:07 am on Nov 8, 2004 (gmt 0)

10+ Year Member



Ok, I already know that you can call on an external javascript from inside HTML coding like so:

<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]

GeorgeGG

4:28 am on Nov 8, 2004 (gmt 0)

10+ Year Member



<script TYPE="text/javascript">
document.write("<script type=\"text/JavaScript\"
src=\"URL_Or_Path_To_Other.js\"></"+"script>");
</script>

GeorgeGG

Rambo Tribble

5:53 am on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe the specification requires all scripts to load with the page, hence, in theory, it shouldn't be possible to load scripts after the page has finished loading. That said, this will fool IE and Opera (but not Mozilla):

<!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.

kirkplug

9:06 pm on Nov 9, 2004 (gmt 0)

10+ Year Member



Rambo, you sure know your stuff. Thanks a heap.

By the way, why is it <script type="text/javascript">
instead of <script language="javascript">?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month