First, the grammar:
Loadscript (Url[,callback])
Or
Loadscript (Settings)
Second, the settings supported parameters:
URL: Script Path
Async: Asynchronous, default false (HTML5)
CharSet: File Encoding
Cache: Caching, default to True
Success: A function that executes after the load succeeds, giving priority to executing the callback.
Third, the invocation example:
Copy Code code as follows:
Loadscript (Url[,callback])
Loadscript ("Http://code.jquery.com/jquery.js");
Loadscript ("Http://code.jquery.com/jquery.js", function () {
Console.log (1)
});
Loadscript (Settings)
Loadscript ({"url": "Http://code.jquery.com/jquery.js", "async": false, "CharSet": "Utf-8″," cache ": false});
Loadscript ({"url": "Http://code.jquery.com/jquery.js", "async": false, "CharSet": "Utf-8″," Success ": function () {
Console.log (2)
}});
Or you can purple:
Loadscript (Settings[,callback])
Loadscript ({"url": "Http://code.jquery.com/jquery.js", "async": false, "CharSet": "Utf-8″},function () {
Console.log ($)
});
Four, the source code:
Copy Code code as follows:
function Loadscript (url,callback) {
var head = Document.head | | document.getElementsByTagName ("Head") [0] | | Document.documentelement,
Script
Options
if (typeof url = = = "Object") {
options = URL;
url = undefined;
}
s = Options | | {};
url = URL | | S.url;
Callback = Callback | | s.success;
Script = document.createelement ("script");
Script.async = S.async | | False
Script.type = "Text/javascript";
if (S.charset) {
Script.charset = S.charset;
}
if (S.cache = = False) {
url = url+ (/\?/.test (URL)? "&": "?" + "_=" + (new Date ()). GetTime ();
}
script.src = URL;
Head.insertbefore (script, head.firstchild);
if (callback) {
Document.addeventlistener? Script.addeventlistener ("Load", callback, false): Script.onreadystatechange = function () {
if (/loaded|complete/.test (script.readystate)) {
Script.onreadystatechange = null
Callback ()
}
}
}
}