Monday
Nov 20 2006
JavaScript Includes Revisited
I've given in in the JavaScript and Modules matter. I'm doing now what all the cool kids are doing: Using XMLHttpRequest synchronously to load external JavaScript files. This has the advantage that it works synchronously, a very important thing for includes. Therefore I don't need callback and moduleLoaded hackery anymore. Also I have sensible error checking, i.e. I can notice when loading a JavaScript module failed. This enables me to have multiple search paths, for example, and sensible error handling.
But it comes at a cost, too. XMLHttpRequest is only supported by "modern browsers". But since moderns browsers means any browser of the last five years I don't care too much. When XMLHttpRequest is not supported, I suppose I can't rely on other useful stuff neither. The synchronous requests also mean that there might be a slight performance hit. But the advantages of synchronous includes outweigh this by far. Finally, XMLHttpRequest doesn't provide the means to recognize when a synchronous request is stalled. This is not of too much concern, since a stalled request usually means that an include files can't be downloaded and so the JavaScript application is not usable anyways. It might be a concern for sensibly degrading, though.
Update: Here is the code to the updated module. Using it is now as simple as this:
<script src="include-http.js"></script>
<script>
include.include("my.module");
myfunc(...);
</script>
Comments
Your new code
by Paul Nahay
Saturday, 2006-11-25 03:18
Thanks, but you must have created your include-http.js file on a Mac. Downloading your code, I don't get correct newlines, so it's all a big much. Can you post code as output into an HTML page so folks can copy and paste and get the newlines they need?
Still doesn't work
by Paul Nahay
Saturday, 2006-11-25 03:26
You code still doesn't work for me in Firefox on WinXP.
Comments for this article have been disabled.