How to apply the script when specifying innerHTML in AJAX _ajax related

Source: Internet
Author: User
Tags eval
In fact, this article has nothing to do with Ajax, but because it is the problem encountered in the AJAX project. So .... innerHTML the script is not run when the assignment is done, you can extract the script using the regular expression, and then eval (script);


Sample code

<script type= "Text/javascript" >
<!--
function Load1 () {
var content= document.getelementbyid ("Content");
Alert ("Load1");
}
function Load2 () {
var content= document.getelementbyid ("Content");
Content.innerhtml= "Load2:<script>load1 ();<\/script>";
}

function Load3 () {
var content= document.getelementbyid ("Content");

Why JavaScript must be after a valid text label, but also talk about JS in IE and Firefox differences. In script block

If there is no valid text label before, IE will make an error. But Firefox works, God, save ie.
var str= "<a>javascript must be </a><script after a valid text label type=\" text/javascript\ ">load1 () <\/script > ";
CONTENT.INNERHTML=STR;
var regexp=/<script.*> (. *) <\/script>/gi;
if (Regexp.test (content.innerhtml)) {
eval (regexp.$1);
}

The following code is set to match multiple script blocks, but unfortunately it is not used in IE

can only run in Firefox, because enough, so did not write down interested can complete it, I JS did not learn it
var matcharray=str.match (REGEXP);
if (Matcharray) {
for (Var i=0;i<matcharray.length;i++) {
if (Regexp.test (Matcharray[i])) {
alert (regexp.$1);
//        }
else{
Alert (Matcharray[i]);
//        }
//    }
//}

}


-->
</script>
<body>
<div id= "Content" >
</div>
<input type= "button" onclick= "Javascript:load1 ();" value= "Load1"/>
<input type= "button" onclick= "javascript:load2 ();" value= "Load2"/>
<input type= "button" onclick= "Javascript:load3 ();" value= "Load3"/>
</body>



Description

Load1

Of course you can run it properly.

In Load2

The script in the content.innerhtml is not run when the value is assigned

In Load3

Get the script block out of innerHTML and eval it to run normally

Maybe who has a better way, please mail to me blackant@tt88.cn


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.