Dynamically execute js code in the ajax request

Source: Internet
Author: User

This article will introduce a js Code for dynamically executing ajax requests.

Index.html

The Code is as follows: Copy code

<Script src = "/js/jquery. js"> </script>
<Script>
Function evalScript (html ){
// Var html = response. responseText;
Var hd = document. getElementsByTagName ("head") [0];
Var re = /(? : <Script ([^>] *)?>) (N | r | .)*?) (? : </Script>)/ig;
Var srcRe =/ssrc = (['"]) (. *?) 1/I;
Var typeRe =/stype = (['"]) (. *?) 1/I;
Var match;
While (match = re.exe c (html )){
Var attrs = match [1];
Var srcMatch = attrs? Attrs. match (srcres): false;
If (srcMatch & srcMatch [2]) {
Var s = document. createElement ("script ");
S. src = srcMatch [2];
Var typeMatch = attrs. match (typeRe );
If (typeMatch & typeMatch [2]) {
S. type = typeMatch [2];
}
Hd. appendChild (s );
} Else if (match [2] & match [2]. length> 0 ){
If(window.exe cScript ){
Window.exe cScript (match [2]);
} Else {
Window. eval (match [2]);
}
}
}
}

$ (Function (){
Pai.get(' B .html ', {}, function (html ){
EvalScript (html );
Alert (sysmsg );
})
})
</Script>

Ajax.html

The Code is as follows: Copy code

<Script type = "text/javascript" src = "test. js"> </script>
<Script type = "text/javascript" src = "ts. js"> </script>
<Script type = "text/javascript"> // <! [CDATA [
Alert ('aaa ');
//]> </Script>
<Script type = "text/javascript"> // <! [CDATA [
Var sysmsg = '1 ';
//]> </Script>

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.