在Internet Explorer的新窗口中运行javascript代码

在Internet Explorer的新窗口中运行javascript代码,javascript,internet-explorer,Javascript,Internet Explorer,我试图在一个空白的新弹出窗口中运行一些注入的javascript代码 var popup = window.open('', 'name', options); var scriptElement = document.createElement('script'); scriptElement.type = 'text/javascript'; scriptElement.text = scriptSource; popup.document.body.appendChild(scriptEl

我试图在一个空白的新弹出窗口中运行一些注入的javascript代码

var popup = window.open('', 'name', options);
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.text = scriptSource;
popup.document.body.appendChild(scriptElement);
javascript代码在FF和Chrome中工作,但我在IE(11)中收到一个
HierarchyRequestError
。我已经找到了另一种方法来编写最后一行,使用这个
popup.document.head.innerHTML=scriptElement.outerHTML但在这种情况下,javascript在任何浏览器中都无法识别。

在PhpDebugToolbar中,我使用以下代码片段创建或更新带有自定义内容的自定义弹出窗口

var detail = window.open('', "php_debug_toolbar_window_" + key, "width=800,height=400,status=yes,scrollbars=yes,resizable=yes");
detail.document.write( [
    '<script type="text/javascript">',
    'if (!document.body) {document.write("<html><head><title></title></head><' + 'body></' + 'body></html>"); }',
    'document.getElementsByTagName("title")[0].innerHTML = ' + JSON.stringify(title) + ';',
    'var content = document.getElementById("content");', 'if (content) {', '    document.body.removeChild(content);', '}',
    'content = document.createElement("div");', 'content.id="content";', 'content.innerHTML = ' + JSON.stringify(html) + ';',
    'document.body.appendChild(content);', '<' + '/script>'
].join("\n"));
var detail=window.open(“”,“php_调试_工具栏_窗口_”+键,“宽度=800,高度=400,状态=yes,滚动条=yes,可调整大小=yes”);
详细资料。文件。填写([
'',
'如果(!document.body){document.write(“”;}',
'document.getElementsByTagName(“标题”)[0].innerHTML='+JSON.stringify(标题)+';',
'var content=document.getElementById(“content”);','if(content){','document.body.removeChild(content);','}',
“content=document.createElement(“div”);”,“content.id=“content”;”,“content.innerHTML=”+JSON.stringify(html)+“;”,
'document.body.appendChild(content);','
].join(“\n”);
重要部分包括:

  • 使用document.write初始化html>head+html>body结构
  • 为您的内容使用#content(或任何其他id)div
  • JSON.stringify您的内容并使用JS中的#content div的.innerHTML属性设置它

到目前为止,这对我来说效果很好,所以我相信你也可以像这样添加脚本标记。

我被迫使用javascript inception在IE中实现这一点,真是太遗憾了。我从没想过这样的修复会奏效,谢谢!