[Go] dynamic load JS script

Source: Internet
Author: User

dynamically loading JS scriptsThere are 4 ways to implement a dynamically loaded JS script:

1, Direct document.write
<script language= "JavaScript" >

document.write ("<script src= ' test.js ' ><\/script>");

</script>



2. Dynamically change the SRC attribute of the existing script
<script src= "id=" S1 "></script>

<script language= "JavaScript" >

S1.src= "Test.js"

</script>



3. Dynamic creation of script elements
<script>

var ohead = document.getelementsbytagname (' HEAD '). Item (0);

var oscript= document.createelement ("script");

Oscript.type = "Text/javascript";

Oscript.src= "Test.js";

Ohead.appendchild (Oscript);

</script>



These three methods are executed asynchronously, that is, when the script is loaded, the main page of the script continues to run, if the above method, the following code will not get the desired effect.

To dynamically load the JS script: A.js, the following is the contents of the file.
var str = "China";

Alert ("This is a variable in the a.js:" + str);





Main Page code:


<script language= "JavaScript" >

function Loadjs (ID, FILEURL)

{

var Scripttag = document.getElementById (ID);

var ohead = document.getelementsbytagname (' HEAD '). Item (0);

var oscript= document.createelement ("script");



if (Scripttag) ohead.removechild (Scripttag);

Oscript.id = ID;

Oscript.type = "Text/javascript";

Oscript.src=fileurl;

Ohead.appendchild (Oscript);

}



Loadjs ("A.js");



Alert ("The main page dynamically loads the A.js and takes the variables:" + str);

</script>

After execution of the above code A.js alert executes and pops up the message,



But there was an error on the main page and there was no popup dialog box. The reason is ' str ' is undefined, why? Because the main page of the a.js is not fully loaded when the STR is taken. You can use the following fourth method when you encounter a script that needs to be executed synchronously.

4, principle: Use XMLHTTP to get the content of the script, and then create a script object.

Note: A.js must be saved with UTF8 encoding, without error. Because the server and XML use UTF8 encoding to transfer data.

Main Page code:
<script language= "JavaScript" >

function Gethttprequest ()

{

if (window. XMLHttpRequest)//Gecko

return new XMLHttpRequest ();

else if (window. ActiveXObject)//IE

return new ActiveXObject ("Msxml2.xmlhttp");

}



function ajaxpage (sId, url) {

var oxmlhttp = Gethttprequest ();



Oxmlhttp.onreadystatechange = function ()

{

if (oxmlhttp.readystate = = 4)

{

if (oxmlhttp.status = = | | oxmlhttp.status = = 304)

{

Includejs (sId, URL, oxmlhttp.responsetext);

}

Else

{

Alert (' XML request error: ' + oxmlhttp.statustext + ' (' + oxmlhttp.status + ') ');

}

}

}



Oxmlhttp.open (' GET ', url, true);

Oxmlhttp.send (NULL);

}



function Includejs (sId, FILEURL, source)

{

if (Source = null) && (!document.getelementbyid (sId))) {

var ohead = document.getelementsbytagname (' HEAD '). Item (0);

var oscript = document.createelement ("script");



Oscript.language = "JavaScript";

Oscript.type = "Text/javascript";

Oscript.id = sId;

Oscript.defer = true;

Oscript.text = source;



Ohead.appendchild (Oscript);

}

}



Ajaxpage ("SCRA", "b.js");



Alert (the main page dynamically loads the JS script. ");

Alert ("The main page dynamically loads the A.js and takes the variables:" + str);

</script>

Now the dynamic loading of a JS script is completed.

$ (document). Ready (function () {
Async is synchronous when false
DataType has helped you add the return result to the document as a script-type DOM element, and if cross-domain, post is converted to get
$.ajax ({
Type: ' GET ',
Url:jsurl,
Async:false,
DataType: ' Script '})
})

[Go] dynamic load JS script

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.