我正在尝试在空白的新弹出窗口中运行一些注入的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在任何浏览器中都无法识别。
答案 0 :(得分:1)
在https://github.com/DracoBlue/PhpDebugToolbar/blob/master/pub/PhpDebugToolbar.js#L913的PhpDebugToolbar中,我使用以下代码段来创建或更新包含自定义内容的自定义PopUp。
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"));
重要部分:
到目前为止,这对我很有用,所以我相信你也可以像这样添加你的脚本标签。