IE8 dynamically create script label onload invalid solution _javascript tips

Source: Internet
Author: User
Tags script tag

The example in this article describes a workaround for dynamically creating a script label onload in IE8. Share to everyone for your reference. The specific analysis is as follows:

To do the project today, I found a strange problem, the dynamically created script tag cannot trigger the OnLoad event under IE8.

The code is as follows:

Copy Code code as follows:
var loadjs = function (src, fun) {
var script = null;
Script = document.createelement ("script");
Script.type = "Text/javascript";
SCRIPT.SRC = src;
if (typeof fun = = "function") {
Script.onload = fun;
}

document.getElementsByTagName ("Head") [0].appendchild (script);
};

Loadjs ("Js/jquery-1.11.0.min.js", function () {
Console.log ("from JQuery");
});

Loadjs ("Test.js", function () {
Console.log ("from Test.js");
});
Test.js:
Console.log (typeof JQuery);

Run Result:
Copy Code code as follows:
Undefined//Test.js runtime, jquery is not loaded
>> typeof jquery/from the console, but found the jquery object, proving the loading sequence problem
"Function"

And the above code Script.onload did not execute, obviously code has been loaded in, why or the onload do not do it? A search on the internet found that many front-end developers encountered this thorny problem, and found a number of alternatives, as follows:
Copy Code code as follows:
var loadjs = function (src, fun) {
var script = null;
Script = document.createelement ("script");
Script.type = "Text/javascript";
SCRIPT.SRC = src;
if (typeof fun = = "function") {
Script.onreadystatechange = function () {
var r = script.readystate;
Console.log (src + ":" + R);
if (r = = ' Loaded ' | | | r = = ' complete ') {
Script.onreadystatechange = null;
Fun ();
}
};
}

document.getElementsByTagName ("Head") [0].appendchild (script);
};

Execution results:
Copy Code code as follows:
Undefined
Js/jquery-1.11.0.min.js:loading
Test.js:complete
From Test.js
js/jquery-1.11.0.min.js:loaded
From JQuery

Implementation steps for, this is similar to the OnLoad function is found, but there is a problem, it is not in order to load, when the jquery file loading, Test.js has been complete, and the first line of the first implementation of the Test.js content. Because Test.js prior to jquery, the undefined was played out. So we can rewrite it like this and let it load linearly:
Copy Code code as follows:
Loadjs ("Js/jquery-1.11.0.min.js", function () {

Console.log ("from JQuery");

Loadjs ("Test.js", function () {
Console.log ("from Test.js");
});
});

Execution results:
Copy Code code as follows:
Js/jquery-1.11.0.min.js:loading
js/jquery-1.11.0.min.js:loaded
From JQuery
function
Test.js:complete
From Test.js

This time, the order of execution is exactly the order we ordered, but the above code looks very awkward, needs layers of nesting, and then found this writing:
Copy Code code as follows:
var loadjs = function (src, fun) {
var script = null;
Script = document.createelement ("script");
Script.type = "Text/javascript";
SCRIPT.SRC = src;
if (typeof fun = = "function") {
Script.onreadystatechange = function () {
var r = script.readystate;
Console.log (src + ":" + R);
if (r = = ' Loaded ' | | | r = = ' complete ') {
Script.onreadystatechange = null;
Fun ();
}
};
}

document.write (script.outerhtml);
document.getElementsByTagName ("Head") [0].appendchild (script);

};

Loadjs ("Js/jquery-1.11.0.min.js", function () {
Console.log ("from JQuery");
});

Loadjs ("Test.js", function () {
Console.log ("from Test.js");
});

The order in which results are executed is also different:
Copy Code code as follows:
function
js/jquery-1.11.0.min.js:loaded
From JQuery
test.js:loaded
From Test.js

If you change the load sequence
Copy Code code as follows:
Loadjs ("Test.js", function () {
Console.log ("from Test.js");
});

Loadjs ("Js/jquery-1.11.0.min.js", function () {
Console.log ("from JQuery");
});

The execution results are different, similar in order loading:
Copy Code code as follows:
Undefined
test.js:loaded
From Test.js
js/jquery-1.11.0.min.js:loaded
From JQuery

I hope this article will help you with your JavaScript programming.

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.