var js = document.createElement('script');
js.src = 'myscript.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(js);
Here's the same test from above, modified to use the script node technique. Note that the third row in the image takes just as long to download, but the other resources on the page are loading simultaneously:
[img]http://yuiblog.com/assets/non-block-yui/2.png[/img]
[url=http://yuiblog.com/assets/non-block-yui/after.html]Test example[/url]
As you can see the script file no longer blocks the downloads and the browser starts fetching the other components in parallel. And the overall response time is cut in half.
[h1]Dependencies[/h1]
A problem with including scripts dynamically would be satisfying the dependencies. Imagine you're downloading 3 scripts and [code]three.js[/code] requires a function from [code]one.js[/code]. How do you make sure this works?
Well, the simplest thing is to have only one file, this way not only avoiding the problem, but also improving performance by minimizing the number of HTTP requests ([url=http://developer.yahoo.com/performance/rules.html#num_http]performance rule #1[/url]).
If you do need several files though, you can attach a listener to the script's [code]onload[/code] event (this will work in Firefox) and the [code]onreadystatechange[/code] event (this will work in IE). Here's a [url=http://www.phpied.com/javascript-include-ready-onload/]blog post[/url] that shows you how to do this. To be fully cross-browser compliant, you can do something else instead: just include a variable at the bottom of every script, as to signal “I'm ready”. This variable may very well be an array with elements for every script already included.
[h1]Using YUI Get utility[/h1]
The [url=http://developer.yahoo.com/yui/get/]YUI Get Utility[/url] makes it easy for you to use script includes. For example if you want to load 3 files, [code]one.js[/code], [code]two.js[/code] and [code]three.js[/code], you can simply do:
var urls = ['one.js', 'two.js', 'three.js']; YAHOO.util.Get.script(urls);YUI Get also helps you with satisfying dependencies, by loading the scripts in order and also by letting you pass an [code]onSuccess[/code] callback function which is executed when the last script is done loading. Similarly, you can pass an [code]onFailure[/code] function to handle cases where scripts fail to load.
var myHandler = {
onSuccess: function(){
alert(':))');
},
onFailure: function(){
alert(':((');
}
};
var urls = ['1.js', '2.js', '3.js'];
YAHOO.util.Get.script(urls, myHandler);
Again, note that YUI Get will request the scripts in sequence, one after the other. This way you don't download all the scripts in parallel, but still, the good part is that the scripts are not blocking the rest of the images and the other components on the page. [url=http://developer.yahoo.com/yui/examples/get/get-script-basic.html]Here's a good example and tutorial on using YUI Get to load scripts[/url].
YUI Get can also include stylesheets dynamically through the method [code]YAHOO.util.Get.css()[/code] [[url=http://developer.yahoo.com/yui/examples/get/get-css-basic.html]example[/url]].
Which brings us to the next question:
[h1]And what about stylesheets?[/h1]
Stylesheets don't block downloads in IE, but they do in Firefox. Applying the same technique of dynamic inserts solves the problem. You can create dynamic link tags like this:
var h = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.href = 'mycss.css';
link.type = 'text/css';
link.rel = 'stylesheet';
h.appendChild(link);
This will improve the loading time in Firefox significantly, while not affecting the loading time in IE.
Another positive side effect of the dynamic stylesheets (in FF) is that it helps with the progressive rendering. Usually both browsers will wait and show blank screen until the very last piece of stylesheet information is downloaded, and only then they'll start rendering. This behavior saves them the potential work of re-rendering when new stylesheet rules come down the wire. With dynamic [code]<link>[/code]s this is not happening in Firefox, it will render without waiting for all the styles and then re-render once they arrive. IE will behave as usual and wait.
But before you go ahead and implement dynamic [code]<link>[/code] tags, consider the violation of the rule of separation of concerns: your page [i]formatting[/i] (CSS) will be dependent on [i]behavior[/i] (JS). In addition, this problem is going to be addressed in future Firefox versions.
[h1]Other ways?[/h1]
There are other ways to achieve the non-blocking scripts behavior, but they all have their drawbacks.
| Method | Drawback |
|---|---|
| Using [code]defer[/code] attribute of the [code]script[/code] tag | IE-only, unreliable even there |
| Using [code]document.write()[/code] to write a script tag | [list=1] [*]Non-blocking behavior is in IE-only [/*][*][code]document.write[/code] is not a recommended coding practice [/*][/list] |
| [code]XMLHttpRequest[/code] to get the source then execute with [code]eval()[/code]. | [list=1] [*]“[code]eval()[/code] is evil” [/*][*]same-domain policy restriction [/*][/list] |
| XHR request to get the source, create a new script tag and set its content | [list=1] [*]more complex [/*][*]same-domain policy [/*][/list] |
| Load script in an iframe | [list=1] [*]complex [/*][*]iframe overhead [/*][*]same-domain policy [/*][/list] |
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有