/**
* II. dyloader dynamically loads JavaScript files
* @ Param JS {string}: complete path of the JS File
* @ Param ID {string}: ID of the Javascript file
* @ Param callback {function}: The CB after loading the Javascript file
* @ Return no return value
*/
Ii. dyloader = function (JS, ID, callback)
{
VaR scriptid = Document. getelementbyid (ID );
If (scriptid)
{
If (callback)
Callback ();
}
Else {
VaR script = Document. createelement ("script ");
Script. ID = ID;
Script. type = "text/JavaScript ";
Script. onload = script. onreadystatechange = function ()
{
If (script. readystate & script. readystate! = 'Loaded' & script. readystate! = 'Complete ')
{
Return;
}
Script. onreadystatechange = script. onload = NULL;
If (callback)
Callback ();
};
Script. src = JS;
VaR head = Document. getelementsbytagname ('head'). Item (0 );
Head. appendchild (SCRIPT );
}
};
Note:
Several ideas for loading JS files dynamically
No. 1 direct document. Write
<Script language = "JavaScript">
Document. Write ("<SCRIPT src = 'test. js'> <\/SCRIPT> ");
</SCRIPT>
No. 2 dynamically change the src attribute of an existing script
<SCRIPT src = ''id =" S1 "> </SCRIPT>
<Script language = "JavaScript">
S1.src = "test. js"
</SCRIPT>
No. 3 dynamically create script elements
<SCRIPT>
VaR S2 = Document. createelement ("script ");
S2.src = "test. js"
Document. Body. insertadjacentelement ("beforebegin", S2 );
</SCRIPT>
Prototype-based scriptaculous (it seems to be written in this way)
Use document. Write when loading. The previous comment indicates that dynamic DOM nodes are not supported by safari2.0.
Based on this
/**
* II. staticloader dynamically loads JavaScript files
* @ Param JS {string}: complete path of the JS File
* @ Return no return value
*/
Ii. staticloader = function (JS ){
Document. writeln ("<scri" + "PT src = '" + JS + "'Type = 'text/JavaScript'> </SC" + "ript> ");
};
We have the static loader method. But there is a problem.
Once the document. Write method is executed, HTML-> body is overwritten, leading to blank page
The correct method is to close <SCRIPT> immediately after loading an external file.
However, in this way, we have another problem. In this way, the loading of JS files is not dynamic. That is to say, we can't do this by using II. staticloader ('A. js ');
Ii. Loading ....
Only
Ii. staticloader ('A. js ');
</SCRIPT>
<SCRIPT>
Ii. Loading ....
</SCRIPT>
What is the difference between the above method and the following one.
<SCRIPT type = 'text/JavaScript 'src = 'a. js'> </SCRIPT>
Therefore, the document. Write method is discarded.
Continue to explore the DOM node approach,
That's the final result .. But I don't know if safari2.0 is supported.
In fact, the minimum download speed is safari3.0, which currently uses 4.0
Test:
Load the JS file that is not loaded on the homepage and call
Ii. dyloader ("http://static.zhongle.com/js/Guide.js", "Guide", function () {alert (Guide. Author );});
The call is successful in FF, IE6, safari3, and safari4. A prompt is displayed.