JS implementation perfect include load implementation code

Source: Internet
Author: User

Use the Include ("...") directly in a. Web effects. /js/common.js ");

Question of multiple requests
Using the include above looks cool, but it brings another 1 serious problems, that is, sending more than 1 Ajax requests.
We often merge JS for Web performance and reduce requests. But using include requires a lot of requests. If this problem is not resolved, it is believed that many people will not use include in the formal product unless it is a LAN product.

How to solve the problem of multiple requests, I also think for a long time, and finally feel the use of client-only JS is no way to solve. So the idea is to use the server-side code to solve
Remember me before an article introduction "JS, CSS Tutorial merge, compress, cache management", the server-side code in the program when the start to merge JS.

So I added the solution to include multiple requests. is to find all of the JS when the program is started, and find that the include is used to replace the include Common.js source code in the Include function. So a.js in the runtime there is no include function, but really contains common.js content of the JS file
Get absolute path based on relative path

function GetPath (Relativepath,absolutepath) {
var reg = new RegExp ("\.\./", "G");
var uplaycount = 0; The number of times the upper level is returned in the relative path.
var m = Relativepath.match (reg);
if (m) uplaycount = M.length;
var lastindex = absolutepath.length-1;
for (Var i=0;i<=uplaycount;i++) {
lastindex = Absolutepath.lastindexof ("/", lastindex);
}
Return Absolutepath.substr (0,lastindex+1) + relativepath.replace (Reg, "");
}
function include (JSSRC) {
First get the src of the current a.js. Call include in A.js to get the last 1 script tags directly is the a.js reference.
var scripts = document.getElementsByTagName ("script");
var lastscript = scripts[scripts.length-1];
var src = lastscript.src;
if (Src.indexof ("http://")!=0 && Src.indexof ("/")!=0) {
A.js use relative path, first replace absolute path
var url = location.href;
var index = Url.indexof ("?");
if (index!=-1) {
url = url.substring (0, index-1);
}
src = GetPath (src,url);
}
var Jssrcs = jssrc.split ("|"); Can include multiple JS, separated by |
for (Var i=0;i<jssrcs.length;i++) {
Load JS using Juqery's synchronized Ajax.
Use document.write dynamically added JS will be in the current JS behind, there may be JS reference problem
Dynamically creating script scripts, non-blocking downloads, and reference problems
$.ajax ({type: ' Get ', Url:getpath (JSSRC,SRC), Async:false,datatype: ' 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.