Javascript 打开一个新选项卡/窗口并向其写入内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11965087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:51:38  来源:igfitidea点击:

Open a new tab/window and write something to it?

javascriptfirefoxfirefox-addonxpcom

提问by Teiv

I'm using Execute JSto write and test Javascript code within Firefox. I want to open a new tab/window and write something to it and I tried

我正在使用Execute JS在 Firefox 中编写和测试 Javascript 代码。我想打开一个新选项卡/窗口并向其写入一些内容,我试过了

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow("navigator:browser");
printWindow = win.open("about:blank");
printWindow = wm.getMostRecentWindow("navigator:browser");
printWindow.gBrowser.selectedBrowser.contentDocument.write('hello');

And

myWindow=window.open('','','width=200,height=100')
myWindow.document.write("<p>This is 'myWindow'</p>")
myWindow.focus()

However I always get this error

但是我总是收到这个错误

[Exception... "The operation is insecure." code: "18" nsresult: "0x80530012 (SecurityError)"

[例外……“操作不安全。” 代码:“18”nsresult:“0x80530012(安全错误)”

Is there any way to get through this exception?

有没有办法解决这个异常?

回答by Wladimir Palant

Edit: As of 2018, this solution no longer works. So you are back to opening about:blankin a new window and adding content to it.

编辑:截至 2018 年,此解决方案不再有效。因此,您将返回about:blank在新窗口中打开并向其中添加内容。

Don't "write" to the window, just open it with the contents you need:

不要“写入”窗口,只需使用您需要的内容打开它:

var data = "<p>This is 'myWindow'</p>";
myWindow = window.open("data:text/html," + encodeURIComponent(data),
                       "_blank", "width=200,height=100");
myWindow.focus();

For reference: data URIs

供参考:数据 URI

回答by James T

Top-level navigation to data URLs has been blocked in Chrome, Firefox (with some exceptions), IE, and Edge (and likely other browsers to boot). They are apparently commonly used for phishing attacks, and major browser vendors decided that the danger outweighed the value provided by legitimate use cases.

Chrome、Firefox(有一些例外)、IE 和 Edge(可能还有其他要启动的浏览器)阻止了到数据 URL 的顶级导航。它们显然常用于网络钓鱼攻击,主要浏览器供应商认为危险超过了合法用例提供的价值。

This Mozilla security blog postexplains that Firefox will block

这篇Mozilla 安全博客文章解释了 Firefox 将阻止

  • Web page navigating to a new top-level data URL document using:
    • window.open("data:…");
    • window.location = "data:…"
    • clicking <a href="data:…">(including ctrl+click, ‘open-link-in-*', etc).
  • Web page redirecting to a new top-level data URL document using:
    • 302 redirects to "data:…"
    • meta refresh to "data:…"
  • External applications (e.g., ThunderBird) opening a data URL in the browser
  • 使用以下命令导航到新的顶级数据 URL 文档的网页:
    • window.open("data:…");
    • window.location = "data:…"
    • 单击<a href="data:…">(包括 ctrl+click、'open-link-in-*' 等)。
  • 使用以下方法重定向到新的顶级数据 URL 文档的网页:
    • 302重定向到 "data:…"
    • 元刷新到 "data:…"
  • 在浏览器中打开数据 URL 的外部应用程序(例如 ThunderBird)

but will not block

但不会阻塞

  • User explicitly entering/pasting "data:…"into the address bar
  • Opening all plain text data files
  • Opening "data:image/*"in top-level window, unless it's "data:image/svg+xml"
  • Opening "data:application/pdf"and "data:application/json"
  • Downloading a data: URL, e.g. ‘save-link-as' of "data:…"
  • 用户明确输入/粘贴"data:…"到地址栏中
  • 打开所有纯文本数据文件
  • 打开"data:image/*"在顶层窗口,除非它是"data:image/svg+xml"
  • 开幕"data:application/pdf""data:application/json"
  • 下载数据:URL,例如“save-link-as” "data:…"

You can also read the proposal to deprecate and remove top-frame navigation to data URLs in Chromeand view the current Chrome status indicating that is has been removed.

您还可以阅读在 Chrome 中弃用和删除顶部框架导航到数据 URL 的建议,并查看当前 Chrome 状态,表明 已删除

As for how to actually open HTML in a new tab or window, this should be sufficient:

至于如何在新标签页或窗口中实际打开 HTML,这应该足够了:

var tab = window.open('about:blank', '_blank');
tab.document.write(html); // where 'html' is a variable containing your HTML
tab.document.close(); // to finish loading the page

Note that at least in Chrome, external scripts injected via document.write might not be loaded on slower connections. That might not be relevant here, but something to watch out for.

请注意,至少在 Chrome 中,通过 document.write 注入的外部脚本可能无法在较慢的连接上加载。这可能与此处无关,但需要注意一些事项。

回答by Diwakar Raja

var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
winPrint.document.write('<title>Print  Report</title><br /><br /> 
Hellow World');
winPrint.document.close();

window.open(uri) does not work in chrome as of 2018

window.open(uri) 自 2018 年起在 chrome 中不起作用