2014-10-07 92 views
0

我试图运行在一个空白的新弹出运行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的一些注入的JavaScript代码,但我收到在IE HierarchyRequestError(11 )。我发现了另一种方式来写这个popup.document.head.innerHTML = scriptElement.outerHTML;的最后一行,但在这种情况下,javascript不会在任何浏览器中识别。

回答

1

In PhpDebugToolbar https://github.com/DracoBlue/PhpDebugToolbar/blob/master/pub/PhpDebugToolbar.js#L913我使用下面的代码片断创建或更新自定义内容的自定义弹出窗口。

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")); 

的重要部分:

  • 使用文件撰写初始化HTML>头+ HTML>车身结构
  • 使用#内容(或任何其它ID)的div为您的内容
  • JSON.stringify你的内容,并用.innerHTML-你的#内容div的属性在JS中

这是为我工作到目前为止相当不错,所以我相信你也可以像这样添加脚本标签。

+0

这是一个耻辱,我不得不做JavaScript的开始,使这项工作在IE浏览器。从来没有想过这样的解决方案可以工作,谢谢! – flaviu 2014-10-08 11:05:17

相关问题