我曾经能够这样做来创建包含一些数据的导出 HTML 页面。但该代码不适用于最新版本的 Google Chrome(它适用于 Chrome 5.0.307.11 beta 和所有其他主要浏览器)。
function createExport(text) {
var target = window.open();
target.title = 'Memonaut - Exported View';
target.document.open();
target.document.write(text);
target.document.close();
}
Chrome 现在会提示域不匹配,并以不安全为由拒绝 JavaScript 调用。在这种情况下如何访问和修改新打开的浏览器选项卡的文档?
最佳答案
我在使用 file://协议(protocol)(在 Linux 下的 Chromium 5.0.342.9(Developer Build 43360)中)使用本地页面时也遇到了这个问题。确切的错误信息是:
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///home/foo/bar/index.htm. Domains, protocols and ports must match.
显然协议(protocol)不匹配,但好消息是:当这个页面在网络服务器上时,Chromium 也会打开一个新窗口作为“about:blank”,但它不再提示了。当使用通过 http://localhost 访问的本地 Web 服务器时,它也可以工作.
编辑: 有错误 filed upstream对这个。根据this comment ,它是固定的,很快就会被卷入主干。
更新:此错误现已修复,以下测试用例正常运行:
var target = window.open();
target.title = 'Memonaut - Exported View';
target.document.open();
target.document.write("test");
target.document.close();
关于javascript - 访问和修改在 Google Chrome 中使用 window.open 打开的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2597979/