Effect of common code running in forums implemented by pure js

Source: Internet
Author: User

Put runcode. js in the website directory when using it, and put the following code where you need to run the code
Copy codeThe Code is as follows:
<Div style = "margin-top: 1em; margin-bottom: 1em"> <div style = "display: none "> {1} </div> <textarea rows =" 12 "cols =" 95 "style = 'font-family:" Courier New ", Courier, monospace; '> </textarea> <script src = "runcode. js "> </script> <br/> <input type =" button "value =" run code "style =" border-left: 1px solid # B1B4CD; border-right: 1px solid #494D74; border-top: 1px solid # B1B4CD; border-bottom: 1px solid #494D74; background: #696D81; color: # FFFFFF; "onclick =" runCode (this) "> <input type =" button "style =" border-left: 1px solid # B1B4CD; border-right: 1px solid #494D74; border-top: 1px solid # B1B4CD; border-bottom: 1px solid #494D74; background: #696D81; color: # FFFFFF; "value =" Copy code "onclick =" copycode (this) "> <input type =" button "style =" border-left: 1px solid # B1B4CD; border-right: 1px solid #494D74; border-top: 1px solid # B1B4CD; border-bottom: 1px solid #494D74; background: #696D81; color: # FFFFFF; "value =" Save code "onclick =" saveCode (this) "> <input type =" button "style =" border-left: 1px solid # B1B4CD; border-right: 1px solid #494D74; border-top: 1px solid # B1B4CD; border-bottom: 1px solid #494D74; background: #696D81; color: # FFFFFF; "value =" Restore code "onclick =" preCode (this) "> tip: you can modify some code before running </div>

You can.
Runcode. js File
Copy codeThe Code is as follows:
// Scripts runcode for BBS ver 2008-06-30, by Bound0 (bound0@veryhman.com)
// First published at http://bbs.blueidea.com
// An example: http://bbs.veryhman.com/showtopic-20094.aspx

Function runCode (which ){
Var p = which. parentNode;
Var OS = p. getElementsByTagName ("textarea ");
If (OS. length = 0) return;
Var o = OS [0];
Var winname = window. open (''," _ blank ",'');
Winname.doc ument. open ('text/html ', 'replace ');
Winname. opener = null;
Winname.doc ument. write (o. value );
Winname.doc ument. close ();
}
Function saveCode (which ){
Var p = which. parentNode;
Var OS = p. getElementsByTagName ("textarea ");
If (OS. length = 0) return;
Var o = OS [0];
Var winname = window. open ('', '_ blank', 'top = 10000 ');
Winname.doc ument. open ('text/html ', 'replace ');
Winname.doc ument. write (o. value );
Winname.document.execcommand('saveas', '', 'code.htm ');
Winname. close ();
}
Function copycode (which ){
Var p = which. parentNode;
Var OS = p. getElementsByTagName ("textarea ");
If (OS. length = 0) return;
Var o = OS [0];
Var is_ie = (userAgent. indexOf ('msie ')! =-1 &&! Is_opera) & userAgent. substr (userAgent. indexOf ('msie ') + 5, 3 );
If (is_ie & o. style. display! = 'None '){
Var rng = document. body. createTextRange ();
Rng. moveToElementText (o );
Rng. scrollIntoView ();
Rng. select ();
Rng.exe cCommand ("Copy ");
Rng. collapse (false );
}
}
Function text (e)
{
Var t = "";
E = e. childNodes | e;
For (var j = 0; j <e. length; j ++)
{

If (e [j]. nodeType! = 1) {t + = e [j]. nodeValue}
Else
{
Var k = e [j]. nodeName;
If (k = 'br '| k = 'P') {t + =' \ r \ n '}
T + = text (e [j]. childNodes)
}
}
Return t;
}
Function preCode (which ){
Var p = which. parentNode;
Var OS = p. getElementsByTagName ("textarea ");
If (OS. length = 0) return;
Var o = OS [0];
Var osv = p. getElementsByTagName ("div ");
If (osv. length = 0) return;
Var ov = osv [0];
Var c =''
If (typeof (ov. innerText )! = "Undefined") {c = ov. innerText} else {c = text (ov )}
O. value = c
}
(Function () {var s = document. getElementsByTagName ("script"); preCode (s [s. length-1])}) ()

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.