Solve the problem that js cannot be found on pages loaded by ajax.

Source: Internet
Author: User

There is A large page named "B" and "c", "B" and "c. The content in c should be changed every time in the menu, but some js files are useful only on pages where c content is changed. Therefore, I wanted to write them in c1, c2, c3, c4, but it cannot be found in it. Later, it can only be written in. however, if some menus are rarely used in this way, page A will be very large, because it is filled with the introduced js. I tried n methods, but none of them worked. Later I wanted to load the required js into the page without clicking the menu, but I just put it in c1, c2, c3, c4... I still can't find it, and then I dynamically put it in the head of A, and then I can find it. Let's solve this problem first.

Function changeRight (url, updateDiv ){
LoadJs (url)
Var url = url;
Var pars = "";
Var myAjax = new Ajax. Updater (updateDiv, url, {method: 'get', parameters: pars });
}

 

Function loadJs (url ){
Var tmp = url. split ("/");
Var tmpNode;
For (var I = 1; I <tmp. length; I ++ ){
If (I = 3 ){
TmpNode = tmp [3]
}
// Alert (tmpNode );
VaR num = tmpnode. indexof (". Do ");
Tmpnode = tmpnode. substring (0, num );
// Alert (tmpnode );

VaR ohead = Document. getelementsbytagname ('head'). Item (0 );
VaR oscript = Document. createelement ("script ");
Oscript. type = "text/JavaScript ";
Oscript. src = ".../../JS/" + tmpnode + ". js ";
VaR tmpvalue = Document. getelementbyid ("flagtmp"). value;
// Alert (tmpvalue );
If (tmpvalue. indexof (tmpnode) =-1 ){
Document. getelementbyid ("flagtmp"). value = Document. getelementbyid ("flagtmp"). Value + "-" + tmpnode;
// Alert ("does not exist! "+ Document. getElementById (" flagtmp "). value +" ------ "+ tmpvalue. indexOf (tmpNode ));
OHead. appendChild (oScript );
}
Else {
// Alert ("already exists ");
}

}
}

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.