Run the Code [html] [/html] (JS only) in Z-Blog)

Source: Internet
Author: User

Yesterday's "using th, colgroup, col to define Table Styles", a careful friend will find that a "run code" link is added: directly click to run the content in Textarea. In fact, there are also related introductions on Blue ideal and 51JS, and there are similar posts on the official Z-Blog Forum, because I do not like to change asp content (upgrade in the future to save replacement ), so I stole a little lazy and made a pure JS version.
It is not time to test whether it is compatible with FireFox. I think the functions of "Save as" and "copy" are redundant, which is also omitted here. If you need to Fix a Bug or technical support, please leave a message. The Code is as follows:
Copy codeThe Code is as follows:
Function RunCode (){
Var ele = document. getElementsByTagName ("textarea ");
For (var I = 0; I <ele. length; I ++ ){
With (ele [I]) {
If (className! = "Code") continue;
Var o = document. createElement ("p ");
Var a = document. createElement ("");
Var em = document. createElement ("em ");
O. className = "runCode ";
A. href = "javascript :;";
A. innerHTML = "Running code ";
A. onclick = function (){
Var win = window. open (''," _ blank ",'');
Win.doc ument. open ('text/html ', 'replace ');
Win.doc ument. writeln (this. parentNode. previussibling. value. replace (/\ u00a0/gi ,""));
Win.doc ument. close ();
}
Em. innerHTML = "(Prompt: you can modify some code before running )";
O. appendChild ();
O. appendChild (em );
InsertAdjacentElement ("afterEnd", o );
}
}
}

Currently, the "run code" function is placed below Textarea. If you want to put it above, modify the following code.
InsertAdjacentElement ("afterEnd", o );
This. parentNode. previussibling. value
Replace (/\ u00a0/gi, "") is used to restore the spaces () and Tab () replaced by the c_function.asp file. In terms of usage, of course, you just need to load the required page in onload. How many "run code" functions are available for Textarea whose className is code?

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.