homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
external Javascripts WITHIN a javascript
kirkplug




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

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




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

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

GeorgeGG

Rambo Tribble




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

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




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

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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved