`
angkorpeach
  • 浏览: 119876 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

运行html代码的js

阅读更多

网上那种运行html代码的js,把要运行的html代码拷贝到文中“your code”的地方就行!

 

<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>

<textarea id="code" rows="20" cols="65">

your code
</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;

 

分享到:
评论
1 楼 charrys 2011-03-01  
非textarea可以运行否?我用了pre不能运行了!

相关推荐

Global site tag (gtag.js) - Google Analytics