Running Javascript in new window.open

前端 未结 4 1923
执念已碎
执念已碎 2020-12-01 22:10

I\'m running this function to open a new window.

function htmlNewWindow(id) {
    var html = $(id).html();
    var newWindow = window.open(\'\');
    newWind         


        
4条回答
  •  醉梦人生
    2020-12-01 22:13

    Scripts added with .innerHTML aren't executed. You need to create a script node and append it to the window's DOM.

    $("#button").click(newWindow);
    
    function newWindow(id) {
      var html = $(id).html();
      var win = window.open('');
      win.document.head.innerHTML = 'Hi';
      win.document.body.innerHTML = '' + html + '';
      var script = document.createElement('script');
      script.src = 'js/myScript.js';
      win.document.head.appendChild(script);
    }
    
    

    This doesn't run in Stack Snippet's sandbox, here's a working jsfiddle.

提交回复
热议问题