I am excited to see that other blogs have the function of Running code from time to time. I think I often don't know how to use JavaScript. Unfortunately, my blog didn't bring this function, so I had to do it myself. The code is relatively simple. Let's take a look and smile.
Usage:
<Textarea class = "runcode"> code to run </textarea>
Here look at the effect of http://dubox.cn/content-26.aspx
Function page_init (){
VaR OBJ = Document. getelementsbytagname ("textarea ");
For (I = 0; I <obj. length; I ++ ){
If (OBJ [I]. classname = "runcode "){
VaR _ temid = "code _" + I;
VaR html = "<textarea style = \" width: 80%; Height: 200px; border-width: 1px; border-style: inset \ "id = \" "+ _ temid +" \ "> ";
HTML + = OBJ [I]. value;
HTML + = "</textarea> <br> ";
HTML + = "<input type = \" button \ "value = \" run code \ "onclick = \" Run ('"+ _ temid + "') \ "style = \" border-width: 1px \ "/> <em> (Note: you can modify the code and run it) </em> ";
VaR newobj = Document. createelement ("Div ");
Newobj. style. Border = "1px solid # c0c0c0 ";
Newobj. style. backgroundcolor = "# f5f7f7 ";
Newobj. style. Padding = "10px ";
Newobj. innerhtml = HTML;
If (document. All ){
OBJ [I]. replacenode (newobj );
} Else {
OBJ [I]. parentnode. replaceChild (newobj, OBJ [I]);
}
}
}
}
Function run (string_id ){
VaR _ code = Document. getelementbyid (string_id). value;
VaR win = Window. Open (''," _ blank ",'');
Win.doc ument. Open ('text/html ', 'replace ');
Win.doc ument. writeln (_ Code );
}
Window. onload = page_init;