JavaScript loadscript Asynchronous Load script example explain _jquery

Source: Internet
Author: User

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 ()
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.