<!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>在线运行HTML/JS代码在线预览html效果</title>
<script type="text/javascript" language="javascript">
var is_ie=document.all;
//运行代码
function runcode(obj) {
var winname=window.open('',"_blank",'');
winname.document.open('text/html','replace');
winname.opener=null;
winname.document.write(obj.value);
winname.document.close();
}
//复制代码
function copycode(obj) {
if(is_ie && obj.style.display!='none') {
var rng=document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
//or target=obj.createTextRange();
rng.execCommand("Copy");
rng.collapse(false);
}
}
//另存代码
function savecode(obj) {
var winname=window.open('','_blank','top=10000');
winname.document.open('text/html','replace');
winname.document.write(obj.value);
winname.document.execCommand('saveas','','code.htm');
winname.close();
}
//剪切代码
function cutcode(obj) {
if(document.all) {
textRange=obj.createTextRange();
textRange.execCommand("cut");
} else {
alert("IE only!");
}
}
//粘贴代码
function pastecode(obj) {
if(document.all) {
textRange=obj.createTextRange();
textRange.execCommand("paste");
} else {
alert("IE only!");
}
}
</script>
</head>
<body>
<form name="form1">
<textarea name="text" id="runcode0" cols="72" rows="9"></textarea><br />
<input type="button" value="运行代码" onClick="runcode(runcode0)">
<input type="button" value="保存代码" onClick="savecode(runcode0)">
<input type="button" value="复制代码" onClick="copycode(runcode0)">
<input type="button" value="剪切代码" onClick="cutcode(runcode0)">
<input type="button" value="粘贴代码" onClick="pastecode(runcode0)">
<input type="button" value="清空代码" onClick="document.form1.text.value='';">
</form>
</body>
</html>
转载请注明:天狐博客 » 在线运行HTML/JS代码预览html效果的实现