用的时候把runcode.js放到网站目录,在需要运行代码的效果的地方放如下的代码
复制代码 代码如下:
<divstyle="margin-top:1em;margin-bottom:1em"><divstyle="display:none">{1}</div><textarearows="12"cols="95"style='font-family:"CourierNew",Courier,monospace;'></textarea><scriptsrc="runcode.js"></script><br/><inputtype="button"value="运行代码"style="border-left:1pxsolid#B1B4CD;border-right:1pxsolid#494D74;border-top:1pxsolid#B1B4CD;;border-bottom:1pxsolid#494D74;background:#696D81;color:#FFFFFF;"onclick="runCode(this)"><inputtype="button"style="border-left:1pxsolid#B1B4CD;border-right:1pxsolid#494D74;border-top:1pxsolid#B1B4CD;;border-bottom:1pxsolid#494D74;background:#696D81;color:#FFFFFF;"value="复制代码"onclick="copycode(this)"><inputtype="button"style="border-left:1pxsolid#B1B4CD;border-right:1pxsolid#494D74;border-top:1pxsolid#B1B4CD;;border-bottom:1pxsolid#494D74;background:#696D81;color:#FFFFFF;"value="另存代码"onclick="saveCode(this)"><inputtype="button"style="border-left:1pxsolid#B1B4CD;border-right:1pxsolid#494D74;border-top:1pxsolid#B1B4CD;;border-bottom:1pxsolid#494D74;background:#696D81;color:#FFFFFF;"value="还原代码"onclick="preCode(this)">提示:您可以先修改部分代码再运行</div>
就可以了。
runcode.js文件
复制代码 代码如下:
//ScriptsruncodeforBBSver2008-06-30,byBound0(bound0@veryhman.com)
//Firstpublishedathttp://bbs.blueidea.com
//Anexample:http://bbs.veryhman.com/showtopic-20094.aspx
functionrunCode(which){
varp=which.parentNode;
varos=p.getElementsByTagName("textarea");
if(os.length==0)return;
varo=os[0];
varwinname=window.open('',"_blank",'');
winname.document.open('text/html','replace');
winname.opener=null;
winname.document.write(o.value);
winname.document.close();
}
functionsaveCode(which){
varp=which.parentNode;
varos=p.getElementsByTagName("textarea");
if(os.length==0)return;
varo=os[0];
varwinname=window.open('','_blank','top=10000');
winname.document.open('text/html','replace');
winname.document.write(o.value);
winname.document.execCommand('saveas','','code.htm');
winname.close();
}
functioncopycode(which){
varp=which.parentNode;
varos=p.getElementsByTagName("textarea");
if(os.length==0)return;
varo=os[0];
varis_ie=(userAgent.indexOf('msie')!=-1&&!is_opera)&&userAgent.substr(userAgent.indexOf('msie')+5,3);
if(is_ie&&o.style.display!='none'){
varrng=document.body.createTextRange();
rng.moveToElementText(o);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
}
functiontext(e)
{
vart="";
e=e.childNodes||e;
for(varj=0;j<e.length;j++)
{
if(e[j].nodeType!=1){t+=e[j].nodeValue}
else
{
vark=e[j].nodeName;
if(k=='BR'||k=='P'){t+='rn'}
t+=text(e[j].childNodes)
}
}
returnt;
}
functionpreCode(which){
varp=which.parentNode;
varos=p.getElementsByTagName("textarea");
if(os.length==0)return;
varo=os[0];
varosv=p.getElementsByTagName("div");
if(osv.length==0)return;
varov=osv[0];
varc=''
if(typeof(ov.innerText)!="undefined"){c=ov.innerText}else{c=text(ov)}
o.value=c
}
(function(){vars=document.getElementsByTagName("script");preCode(s[s.length-1])})()