<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS运行HTML代码:运行代码、复制代码、另存代码、清除代码</title>
<script>
<!–运行代码 –>
function runEx(cod1) {
cod=document.all(cod1)
var code=cod.value;
if (code!=""){
var newwin=window.open('','',''); //打开一个窗口并赋给变量newwin。
newwin.opener = null // 防止代码对页面修改
newwin.document.write(code); //向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
newwin.document.close();
}
}
//复制文本域代码
function cycode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
window.status="高亮度包含的代码已被复制到剪贴板!Contents highlighted and copied to clipboard!"
setTimeout("window.status=''",1800)
rng.collapse(false);
alert("内容已复制到剪切板!");
}
//另存代码
function svcode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.writeln(obj.value);
winname.document.execCommand('saveas','','code.htm');
winname.close();
}
//清空代码
function cleararea(obj){
var box = document.getElementById("code");
box.value="";
}
</script>
<style type="text/css">
.btn02 { width:60px; height:24px; border:1px solid #87a3c1; background:url(http://www.hihaven.cn/wp-content/themes/wp-HavenCMS/images/btn_bg.gif); color:#555; font-size:12px; line-height:180%; cursor:pointer;}
.btn04 { width:60px; height:24px; border:1px solid #a2904d; background:url(http://www.hihaven.cn/wp-content/themes/wp-HavenCMS/images/btn_bg.gif) 0 -66px; color:#630; font-size:12px; cursor:pointer; line-height:180%;}
</style>
</head>
<body>
<div align="left">
<textarea class='fm' id='code' style="width:600px;height:220px;" cols="80" rows="15"></textarea><br />
<input onclick="runEx('code')" name="button" onmouseover=this.className="btn04" onmouseout=this.className="btn02" type="button" value="运行代码" onFocus="this.blur()">
<input onClick="cycode(code)" name="button" onmouseover=this.className="btn04" onmouseout=this.className="btn02" type="button" value="复制代码" onFocus="this.blur()">
<input onclick="svcode(code)" name="button" onmouseover=this.className="btn04" onmouseout=this.className="btn02" type="button" value="另存代码" onFocus="this.blur()">
<input onclick="cleararea(code)" name="button" onmouseover=this.className="btn04" onmouseout=this.className="btn02" type="button" value="清空代码" onFocus="this.blur()">
<span style="color:#999999; font-size:12px;"> 提示:可以先修改部分代码后再运行</span></div>
<br>
<a href="http://www.tc711.com">tc711.com</a>整理
</body>
</html>