用JS在页面内实现预览/执行html代码功能

<textarea name="txtAdCode" id="txtarea" rows="8" cols="40">
这里填入要执行的完整html代码
</textarea><br />
<input type="button" onclick="runCode()" value="运行代码" />
<script type="text/javascript" language="javascript">
function runCode(){ //定义一个运行代码的函数,
         var code = document.getElementById("txtarea").value;//即要运行的代码。
         var newwin = window.open('', '', '');  //打开一个窗口并赋给变量newwin。
         newwin.opener = null // 防止代码对论谈页面修改
         newwin.document.write(code);  //向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
         newwin.document.close();
}</script>

  

原文地址:https://www.cnblogs.com/cn2758/p/3086903.html