On my previous post I looked at how some frameworks handle the multimodule approach to separate javascript code. Since then I’ve been playing with different ways of loading scripts online vs. doing it offline.
After doing some research on the subject I found this website where they show three different approaches to load javascript files. I did some testing and here are some results. The three methods to load the javascript tested were:
Inserting a script tag with a src into the head of the document:
var loadScriptTag = function(src) { var head = document.getElementsByTagName("head")[0]; var script = new Element("script", { type: "text/javascript" }); script.src = src; head.appendChild(script); };
Inserting a script tag with code fetched using ajax into the head of the document:
var loadScriptTagWithAjax = function(src) { var response = new Ajax.Request(src, { method: "get", evalJS: false, asynchronous: false }); var head = document.getElementsByTagName("head")[0]; var script = new Element("script", { type: "text/javascript" }); script.text = response.transport.responseText; head.appendChild(script); };
Using eval to execute the code fetched using ajax:
var loadScriptAjax = function(src) { var response = new Ajax.Request(src, { method: "get", evalJS: false, asynchronous: false }); eval(response.transport.responseText); };
The test code was the following:
var log = function(m) { new Insertion.Bottom(document.body, m + "\n"); }; log("loadScriptTag"); log("Before"); loadScriptTag("test-tag.js"); log("After"); log("loadScriptTagWithAjax"); log("Before"); loadScriptTagWithAjax("test-tag-with-ajax.js"); log("After"); log("loadScriptAjax"); log("Before"); loadScriptAjax("test-ajax.js"); log("After");
This files were loaded using a local Apache server. The results were the following:
Firefox 3.1 Beta 2 on Mac
loadScriptTag
Before
AfterloadScriptTagWithAjax
Before
Test loadScriptTag
Test loadScriptTagWithAjax
AfterloadScriptAjax
Before
Test loadScriptAjax
After
It seems that firefox executes the script with src just before the script with text everytime. Any way it is asynchronous.
Safari 3.2 on Mac
loadScriptTag
Before
AfterloadScriptTagWithAjax
Before
Test loadScriptTag
Test loadScriptTagWithAjax
AfterloadScriptAjax
Before
Test loadScriptAjax
After
Safari works just like firefox.
Opera 10 Alpha on Mac
loadScriptTag
Before
AfterloadScriptTagWithAjax
Before
AfterloadScriptAjax
Before
Test loadScriptAjax
After
Test loadScriptTag
Test loadScriptTagWithAjax
Opera executes the script tags when all the code is done running.
IE 6.0 on Mac using ies4osx
loadScriptTag
Before
AfterloadScriptTagWithAjax
Before
Test loadScriptTagWithAjax
AfterloadScriptAjax
Before
Test loadScriptAjax
After
Test loadScriptTag
IE executes the script tag with src at the end but the script tag with content in the right place.
Overall the only common result is using ajax with eval. This is my choice for modularjs.
on the newest opera, this script can not show… Can you help about that? 40% visitor on my site are come from opera browser.
Thanks 4 your answered before..
Hello RBo, are you using modularjs? Could you try this page and report back the results?