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