js实现运行代码的预览功能
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js实现运行代码的预览功能</title>
</head>
<body>
<textarea id="codeContent" rows="9" cols="50">
在这里输入要运行的代码
</textarea>
<br>
<input type="button" onclick="runCode()" value="运行代码" />
<input type="button" onclick="copyCode()" value="复制代码" />
<input type="button" onclick="saveCode()" value="代码另存为" />
<script language="javascript">
var obj=document.getElementById('codeContent');
function runCode(){
var code=obj.value;
var newWindow=window.open('','','');
newWindow.opener=null;
newWindow.document.write(code);
newWindow.document.close();
}
function saveCode(){
var code=obj.value;
var winname=window.open('','_blank','top=10000');
winname.document.open('text/html','replace');
winname.document.writeln(code);
winname.document.execCommand('saveas','','phpernote.html');
winname.close();
}
function copyCode(){
if(isIE()){
alert('代码已复制到剪贴板!');
}else{
alert('本页面复制功能仅支持IE浏览器!');
return false;
}
var rng=document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
function isIE(){
var Sys={};
var ua=navigator.userAgent.toLowerCase();
var s;
(s=ua.match(/msie ([\d.]+)/))?Sys.ie=s[1]:false;
return Sys.ie;
}
</script>
</body>
</html>
标签:JavaScript