Home > Web Front-end > JS Tutorial > javascript bootstrap_javascript tips

javascript bootstrap_javascript tips

WBOY
Release: 2016-05-16 18:59:16
Original
1457 people have browsed it

1. When the web page is loading, the js file references in the page code (as shown below) will generate http requests to the server, because the files loaded before the body.onload event (here I call them statically loaded files) will generate http requests to the server. . In order to reduce the server's http requests, I advocate that each page has only one js file reference at most.

2. Load the file after the body.onload event (here I call it When loading a file dynamically), the browser will first search for the cache file. If the cache file does not exist, it will make an http request to the server. Therefore, I advocate dynamic loading of files, and the static loading of files before the body.onload event should be precise. short.
3. This system program is divided into single files according to functional modules, and is downloaded on demand during runtime in /source/js/system.js, instead of downloading all script program codes at once, reducing network bandwidth usage. .
4. In order to prevent encoding problems when calling js code across websites, all Chinese characters in the program are encoded with the function escape. If it is only used on a single website, you can change the encoding back to Chinese characters.
5. All functions or classes marked as "for this website's own use" are likely to be related to the data types customized in the background of this website. You can delete it.

The following is the /js/Load.js program and the comments:

Copy code Code As follows:

var IsBody={};

//Define the code for dynamically loading 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];document.getElementsByTagName('head')[0] .appendChild(J);}};"));

//Test whether document.body has been loaded
IsBody.Try=function()
{
if(document.body&& ((this.IsIE=(document.readyState!=null))?document.readyState.toLowerCase()=='complete':true))
{ //After document.body is loaded, determine whether the browser is IE or FoxFire , this program currently supports these two browsers
clearTimeout(this.Interval);
if(typeof(LoadCssFile)!='undefined')
{ //Dynamic loading of css files is also intended to reduce the number of servers http request (note that LoadCssFile here is an array)
var C=LoadCssFile,j=C.length,i=1;
while(i{
var J=document.createElement ('link');J.rel='stylesheet';J.type='text/css';J.href=C[i ];document.getElementsByTagName('head')[0].appendChild(J);
}
}
//Dynamic loading of images, the purpose is also to reduce the server's http requests
if(typeof(LoadImage)!='undefined') this.AppendJs('/source/js/LoadImage .js');
//Dynamic loader body system
this.AppendJs('/source/js/System.js');
}
};
IsBody.Interval= setInterval('IsBody.Try();',100);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template