[Original] adding the code running function to your blog using JavaScript

Source: Internet
Author: User

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;

 

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.