In fact, this article Article It 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 );
ExampleCode
<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