Comparison with XMLHttpRequest()
Advantages
- Can request a file from anywhere on the net, not just the server your page was loaded from.
- Works in IE even when ActiveX is turned off (though not when Javascript is turned off).
- Works with a few older browsers that don't support XMLHttpRequest, like Opera 7 (though not Macintosh versions of IE).
Disadvantages
- Returned data has to be formatted as Javascript code. XMLHttpRequest() can be used to fetch data in any format, XML, JSON, plain text, or whatever.
- Can only do GET requests, not POST requests.
- Whether the request is synchronous or asynchronous is pot luck, depending on the browser. With XMLHttpRequest() you can control this.
- When fetching JSON data from an untrusted source, there is no possiblity of checking the data before feeding it to the Javascript parser. With XMLHttpRequest() you can parse the data with something like json2.js instead of eval() for secure parsing.
Load Only Once
- var script = document.createElement("script");
- script.setAttribute("language", "javascript");
- script.setAttribute("type", "text/javascript");
- script.setAttribute("src", url);
- var head = document.getElementsByTagName("head")[0];
- head.appendChild(script);
Multiple Loads
Cache
First, you need to worry about caching. Typically, the url you're going to be loading is going to be some kind of CGI program. After all, if the output doesn't vary, why bother loading it more than once? Usually browsers are fairly smart about not caching those, but you might want to take some steps to ensure that the second call doesn't just get you the cached copy of the result of the first call. So the CGI should probably be outputting headers to suppress caching. You might also put extra arguments on the URL, perhaps by keeping a count of the number of times you've called it, and adding a "?count=7" argument onto the end of the URL (where 7 is the current count). This makes the URL different on each call, and further ensures that you won't get a cached copy.
Delete Script
Second, you'll tend to accumulate a lot of script tags in your header. This doesn't necessarily do a whole lot of harm, but it seems sloppy. So you could probably delete them by doing head.removeChild(script)
. I think you can actually have the callback function do this immediately after loading. Removing the script tag does not undefine functions or variables that were defined by it, so you are done with it the moment it is done loading.
Detect Load Completion
After we have loaded the helper code, we obviously want to call it to start it running. However, we can't just put a call to complete() after the commands above, because the browser may be loading the "myScript.js" file asynchronously, which means that our call may occur before the function has finished (or even started) loading.
Here we set up two different event handlers on the newly created script tag. Depending on the browser, one or the other of these two handlers is supposed to be called when the script has finished loading. The onreadystatechange
handler works on IE only. The onload
handler works on Gecko browsers and Opera.
The readyState theoretically goes through a series of states:
- 0 uninitialized
- 1 loading
- 2 loaded
- 3 interactive
- 4 complete
- var HTTPUTILITY = {
- getScript: function(url, callback) {
- var isLoaded = false;
- var script = document.createElement("script");
- script.setAttribute("language", "javascript");
- script.setAttribute("type", "text/javascript");
- script.setAttribute("src", url);
- script.onload = script.onreadystatechange = function() {
- if (!isLoaded && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
- isLoaded = true;
- if (typeof callback === "function") {
- callback();
- }
- if (this.tagName.toLowerCase() === "script") {
- document.getElementsByTagName("head")[0].removeChild(this);
- }
- }
- };
- var head = document.getElementsByTagName("head")[0];
- head.appendChild(script);
- }
- };
- function complete() {
- alert("complete");
- }
- HTTPUTILITY.getScript("myScript.js", function(){complete();});
document.write
- document.write('<' + 'script type="text/javascript" src="http://www.lingihuang.com/libraries/library.js"' + '><' + '/script>');
回應 (Leave a comment)