JavaScript Bootstrapper _javascript Tips

Source: Internet
Author: User
1. When the Web page is loaded, the JS file reference in the page code (below) will generate an HTTP request to the server because the file that was loaded before the Body.onload event (which I call static loading) will produce an HTTP request to the server. In order to reduce the server's HTTP request, I advocate that each page has only one JS file reference.
<script language= ' Javascript ' type= ' text/javascript ' src= '? JS ' ></script>
2, After the Body.onload event loads the file (which I call the dynamic loading), the browser first looks for the cached file and sends an HTTP request to the server if the cached file does not exist, so I advocate the dynamic loading of the file, and the static loading file before the Body.onload event should be done in a fine and short order.
3, the System program in accordance with functional modules divided into a single file, in the/source/js/system.js to achieve runtime on-demand download, rather than the horror of a one-time download all the script program code, reduce network bandwidth consumption.
4, in order to allow JS code across the site will not appear coding problems, the program in all Chinese characters with the function escape code, if only a single site use, you can change the code back to Chinese characters.
5, all functions or classes marked as "This site for their own", most likely with the background of the site customized data types, you can delete it.

The following is a/js/load.js program and a note note:

Copy Code code as follows:

var isbody={};

Define code to dynamically load JS program Files
Eval ("isbody.appendjs=" + (isbody.appendjscode= function () {var a= (typeof (arguments[0)) = = ' string '? Arguments: Arguments[0]); for (Var i=a.length-1;i>=0;i--) {var j=document.createelement (' script '); j.language= ' JavaScript '; J.type= ' Text/javascript '; J.src=a[i];d ocument.getelementsbytagname (' head ') [0].appendchild (J);}; "));

Test whether the document.body has been loaded
Isbody.try=function ()
{
if (document.body&& (this. Isie= (document.readystate!=null))? document.readyState.toLowerCase () = = ' complete ': true)
{//document.body after loading to determine whether the browser is IE or Foxfire, this program currently supports both types of browsers
Cleartimeout (this. Interval);
if (typeof (Loadcssfile)!= ' undefined ')
{///dynamically load CSS file to reduce server HTTP request (note here Loadcssfile is a number of arrays)
var C=loadcssfile,j=c.length,i=1;
while (I&LT;J)
{
var j=document.createelement (' link '); j.rel= ' stylesheet '; J.type= ' Text/css '; j.href=c[i++];d ocument.getelementsbytagname (' head ') [0].appendchild (J);
}
}
Dynamically loading pictures for the purpose of reducing server HTTP requests
if (typeof (LoadImage)!= ' undefined ') this. Appendjs ('/source/js/loadimage.js ');
Dynamic Loader principal System
This. Appendjs ('/source/js/system.js ');
}
};
Isbody.interval=setinterval (' Isbody.try (); ', 100);
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.