Art asynchronous script loading

Due to load other elements on the page load can block JavaScript scripts, asynchronous non-blocking way to load a script file is particularly important for the performance of web pages, and even can be said is critical.
Facebook JavaScript SDK configuration example as follows:

<script>(function(d,s,id){
    var js,fjs = d.getElementByTagName(s)[0];
    if(d.getElementById(id)) return;
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js,fjs);
}(document,'script','facebook-jssdk'));</script>

This is a self-executing function, ensures that all temporary variables are in effect in the local, and does not pollute the global namespace.
If you have control over the host page, everything will become easier:

<script>(function(d){
    var js = d.createElement('script');
    js.src = "http://example.com/my.js";
    (d.head || d.getElementByTagName('head')[0]).appendChild(js);
}(document));</script>

Asynchronous non-blocking loading a script of great help for the performance of the web page.

Guess you like

Origin www.cnblogs.com/baimeishaoxia/p/12604083.html