How to apply the SCRIPT in ajax when specifying innerHTML

Source: Internet
Author: User

In fact, this article has nothing to do with ajax, but it is a problem encountered in the ajax project. so .... when the innerHTML value assignment operation is performed, the script won't be run. You can use a regular expression to extract the script and then eval (script );


Sample Code

<Html>
<Head>
<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 does Javascript have to be followed by a valid Text tag? The difference between Javascript in IE and FireFox should be discussed again.

// If there is no valid Text tag, IE will fail. But FireFox can run normally. God, save IE.
Var str = "<a> Javascript must be followed by a valid Text tag </a> <script 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 cannot be used in IE.

//// It can only run on FireFox. Because it is enough, you can complete it if you are not interested in writing it. Even if Javascript is not well written
// 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>
</Head>
<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>
</Html>



[Description]

Load1

Of course it can run normally

Load2

When Content. innerHTML is assigned a value, the Script will not be run.

Load3

The script block in innerHTML can be obtained and run properly only after eval is obtained.

Maybe there's 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.