The example in this article describes how to safely load JavaScript files asynchronously. Share to everyone for your reference. Specifically as follows:
How to use:
(function () {
__safeloadscript ("Http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", function () {
alert (jQuery);
})
();
JavaScript implementation code:
Window.__safeloadscript = function (src, callback) {function addevent (obj, type, fn) {if (O
bj.attachevent) {obj[' E ' + type + fn] = fn;
Obj[type + fn] = function () {obj[' E ' + type + fn] (window.event);}
Obj.attachevent (' on ' + type, Obj[type + fn]);
else Obj.addeventlistener (type, FN, false);
function Async_load (SRC, callback) {var s = document.createelement (' script ');
S.type = ' Text/javascript ';
S.async = true; var protocol = (("https: = = document.location.protocol)?"
https://":" http://");
S.SRC = protocol + src;
var x = document.getelementsbytagname (' script ') [0];
X.parentnode.insertbefore (S, x); S.onload = S.onreadystatechange = function () {if (callback && (!this.readystate | | this.readystate = = "Loade D "| |
This.readystate = = "complete")) {callback ();
}
};
addevent (window, "Load", function () {async_load (SRC, callback);
});
};
The
wants this article to help you with your JavaScript programming.