TEXTAREA 运行代码

<!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=gb2312" />
<title>textarea</title>
<script type="text/javascript">
 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 saveCode(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 copyCode(obj) {
  var rng = document.body.createTextRange();
  rng.moveToElementText(obj);
  rng.scrollIntoView();
  rng.select();
  rng.execCommand("Copy");
  rng.collapse(false);
 }
</script>
</head>

<body>
<textarea id="code" rows="10" cols="95">
<!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=gb2312" />
<title>测试文档</title>
</head>
<body>测试文档
</body>
</html>
</textarea><br>
<input type="button" value="运行代码" onclick="runCode(code)">&nbsp;
<input type="button" value="复制代码" onclick="copyCode(code)">&nbsp;
<input type="button" value="另存代码" onclick="saveCode(code)">&nbsp;
提示:您可以先修改部分代码再运行<br />
</body>
</html>


 

posted @ 2009-06-23 11:51  夜色狼  阅读(212)  评论(0编辑  收藏  举报