打开新窗口并运行代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>'运行代码'的文本域代码</title>
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
</head>
<body>
    <div>
        <textarea id="code_vessel" name="textarea" cols="60" rows="10">
<!DOCTYPE html>
<head>
    <meta charset="utf-8" />
    <title>新窗口</title>
</head>
<body>
    <h1>欢迎来到新窗口!</h1>
</body>
</html>

        </textarea><br>
        <button id="run_code">运行代码</button>
    </div>

  <script>
    //运行文本域代码 原生js实现
    window.onload=function(){
      var oCodeVessel=document.getElementById('code_vessel');
      var oRunCode=document.getElementById('run_code');
      oRunCode.onclick=function(){
        runCode(code_vessel);
      };
      function runCode(cod1) {
        var codeValue = cod1.value;
        if (codeValue != "") {
              //pageURL 为子窗口路径, name为子窗口句柄, parameters为窗口参数(各参数用逗号分隔)
            var newwin = window.open('', '', ''); //打开一个窗口并赋给变量newwin。
            newwin.opener = null; // 防止代码对论坛页面修改
            newwin.document.write(codeValue); //向这个打开的窗口中写入代码,这样就实现了运行代码功能。
            newwin.document.close();
        }
      }
    };

    //运行文本域代码 jquery实现 未做空值判断
    // $(function(){
    //   (function(){
    //     $('#run_code').click(function(){
    //       runCode($('#code_vessel').val());
    //     });
    //     function runCode(val){
    //       var newwin=window.open('','','');//打开新窗口
    //       newwin.opener=null;
    //       newwin.document.write(val);
    //       newwin.document.close();
    //     }
    //   })();
    // });
  </script>
</body>

</html>
原文地址:https://www.cnblogs.com/yuanqiao/p/4269522.html