spacer
Yehuda Shiran February 26, 2001
Dynamically Loading External Files
Tips: February 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Internet Explorer 5.5 and Netscape 6 differ from previous versions in the way they dynamically load external scripts. The term "dynamic loading" refers here to the case where you document.write the include statement on the fly. Suppose you have the following external script:

function printMessage() {
  alert("Hello from External");
}
and you want to dynamically load it from the main html code:

<SCRIPT LANGUAGE="JavaScript">
document.write('<SCRIPT LANGUAGE="JavaScript" SRC="010226b.js"><\/SCRIPT>');
</SCRIPT>

Now, suppose you want to call the printMessage() function in the main html program, immediately after loading the script:

<SCRIPT LANGUAGE="JavaScript">
document.write('<SCRIPT LANGUAGE="JavaScript" SRC="010226b.js"><\/SCRIPT>');
printMessage();
</SCRIPT>

Try it. You get an error message. The reason for this failure is that both Internet Explorer 5.5 and Netscape 6 do not wait for the external script to be read before continuing parsing the main script.

One of the remedies for this problem is to close the <SCRIPT> element immediately after loading the external script:

<SCRIPT LANGUAGE="JavaScript">
document.write('<SCRIPT LANGUAGE="JavaScript" SRC="010226b.js"><\/SCRIPT>');
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
printMessage();
</SCRIPT>
Try it. You should get an alert box with the message "Hello from External".


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint