Running code in Z-blog [html][/html] (Pure JS version) _javascript tips

Source: Internet
Author: User
Yesterday's "Use Th,colgroup,col definition table Style", careful friend will find added a "Run Code" link: Direct click can run textarea content. In fact, the blue ideal, the 51JS also has related introduction, Z-blog official forum also have similar posts, because they do not like to change the content of ASP (in the future upgrade saves the replacement), so stole a little lazy, made a pure JS version of.
is compatible with Firefox, has not yet time to test. I think "Save as" and "copy" function is also more superfluous, here also omitted. If you need fix bugs or technical support, please leave me a message ^_^ specific code as follows:
Copy Code code 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 ("a");
var em = document.createelement ("em");
O.classname = "RunCode";
A.href = "javascript:;";
a.innerhtml = "Run Code";
A.onclick = function () {
var win = window.open ("," "_blank", "");
Win.document.open (' text/html ', ' replace ');
Win.document.writeln (This.parentNode.previousSibling.value.replace (/\u00a0/gi, ""));
Win.document.close ();
}
em.innerhtml = "(Hint: You can modify some of the code before running)";
O.appendchild (a);
O.appendchild (EM);
Insertadjacentelement ("Afterend", O);
}
}
}

Currently the "Run code" function is placed below the textarea, if you want to put above, change the following code.
Insertadjacentelement ("Afterend", O);
This.parentNode.previousSibling.value
The Replace (/\u00a0/gi, "") is primarily intended to restore the space () and tab () that are replaced by the c_function.asp file. The use of course is in the desired page onload come in, how many classname for the code textarea, there are how many "run code" function, simple enough?

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.