> From: Alex Weber
> 
> So just to clear things up, you prefer using document.write() 
> to insert <script> tags instead of appending the elements to the DOM:
> 
>   var finan = document.createElement('script');
>           finan.type = 'text/javascript';
>                 finan.src = 'http://www.mydomain.com/js/mts_finan.js';
>           head.appendChild(finan);
> 
> i guess its more efficient because you don't have to access 
> the document object, but any other particular reasons?

No, that isn't the reason at all. It's because it makes the code much
simpler. You don't have to write any code to load the scripts in their
proper sequence; it happens automatically. <script> tags are evaluated in
the order they appear in the source code, and code you add with
document.write is inserted immediately after the end of the script tag that
does the document.write.

Dynamically inserting a script element with createElement/appendChild is a
great technique - in fact it's the basis for the popular JSONP method of
cross-domain JSON downloads - but it doesn't automatically tell you when the
script is loaded. That why your developer wrote that repeating interval code
- to check when each script is ready and load the next one in sequence.

> Awesome, one huge problem out of the way!

Well, maybe, maybe not... :-) See below...

> We're currently at a point where our server isn't exactly 
> being overloaded so i think its safe to host jquery.min on 
> google code and the plugin (yet to be minified) and custom 
> code possibly combined in 1 file, since there won't be a lot 
> of custom code to begin with.
> 
> Also, to satisfy your curiosity, the whole purpose of using 
> the mts_load.js function is because this code is a part of a 
> banner we're planning to distribute and host in numerous 
> websites, so a lot of them won't want scripts other than 
> their own embedded directly, so we found that calling one 
> simple function (mts_load) that would to all the dirty work 
> was acceptable with them :)

When I recommended document.write, I wasn't really thinking about the whole
context. There may be one good reason to use the dynamic script elements
instead. When you document.write the script tags, the browser will wait
while those script files are loaded. If you use dynamic script elements, the
browser continues loading the rest of the page while the additional scripts
are loaded. The only script it waits for is the main loader script which is
loaded with a script tag.

If this behavior is what you want, then you can still get it without all the
timers and extra complication. Concatenate all of your scripts (except the
loader itself) into a single file. Then simply use a single dynamic script
element to load that entire script. You don't need any timers or sequencing
that way. Simply load that one big script, and the code in it will run in
the order you expect. Very simple, and you still get the benefit of letting
the page continue loading while the secondary scripts are fetched
asynchronously.

-Mike

Reply via email to