Js file introduction implementation code

Source: Internet
Author: User
Tags getscript

It's not easy for them to write a new method because they all reference this AA. js file. after JavaScript code is executed, a function automatically introduces the jq library. Therefore, the method for introducing the js file is as follows:
Copy codeThe Code is as follows:
GetScript: function (s, call ){
Var el = UI. DC ('script ');
If (call ){
El. onload = el. onreadystatechange = call;
}
UI. A (el, 'type', 'text/javascript ');
UI. A (el, 'src', s );
UI. GT (document, 'head') [0]. appendChild (el );
}
/* UI. DC is the creation object, UI. A is the attribute value, and GT is the abbreviation of getElementsByTagName */

So execute UI. getScript ("js/jquery/jquery-1.4.2.min.js", function () {alert ("loaded successfully ")});

The results show that the loading is successful in IE and ff, but when I use jq in HTML, I cannot execute it in IE. I can refresh it and execute it occasionally, there are also differences between static pages on the server and on the client, but there is no problem in Firefox .........

So I thought it was not because the jq file was loaded in parallel with the html file. Before jq was loaded successfully, the HTML file was executed, so I added it at the end of the html body.
Copy codeThe Code is as follows:
<Script>
Alert ("pre-html execution ")
</Script>

It is found that the execution is performed before hmtl is popped up, and then the loading is successful. This is also true under Firefox. When uploading to the server, I feel that the pop-up of Firefox will appear at the same time .....

So I began to wonder how to set it to ensure that the jq file can be executed only after it is loaded, how to add <script type = "text/javascript" src = "js/jquery. js "> </script> ..
I thought that I used to browse a page and used a loading file to introduce many js files. I can use these introduced file functions in html. I found the js file content as follows:

Copy codeThe Code is as follows:
Var Collapsar = {
Version: '0. 0.1 ',
Require: function (libraryName ){
// Inserting via DOM fails in Safari 2.0, so brute force approach
Document. write ('<script type = "text/javascript" src = "' + libraryName + '"> </script> ');
},
Load: function (){
If (typeof Prototype = 'undefined') |
(Typeof Element = 'undefined') |
(Typeof Element. Methods = 'undefined') |
ParseFloat (Prototype. Version. split (".") [0] + "." +
Prototype. Version. split (".") [1]) <1.5)
Throw ("The Prototype JavaScript framework 1.5.0 + is required ");

$ A (document. getElementsByTagName ("script"). findAll (function (s ){
Return (s. src & s. src. match (/loader \. js (\?. *)? $ /))
}). Each (function (s ){
Var path = s. src. replace (/loader \. js (\?. *)? $ /,'');
Var primary des = s. src. match (/\?. * Load = ([a-z,] *)/);
(Des? Des [1]: ""). split (','). each (
Function (include ){
Collapsar. require (path + include + '. js ')});
});
}
}
Collapsar. load ();


<Script type = "text/javascript" src = "js/loder. js? Load = jquery ,...,... "> </script> the file after the equal sign can be introduced. In fact, this sentence plays a key role in document. write ('<script type = "text/javascript" src = "' + libraryName + '"> </script> ');
I just saw some questions. Isn't the write method like adding content in the document? The content should appear in the body tag. The experiment found that if a string does appear in the document, if a tag such as script link appears in the head, add reference js files to the head and load them directly in the head before executing the content in the body .... what is the difference between loading the script and adding it ........

Experiment: Add the following in AA. js:

UI. getsc = (function (){
Document. write ('<script type = "text/javascript" src = "js/jquery/jquery-1.4.2.min.js"> </script> ');
})()

Let him execute automatically. The jq method is introduced in the first line of the body. The experiment is successful, and can be executed in the same way as in IE and ff;
You have time to continue the experiment ...................

Related Article

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.