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> |