打开一个新选项卡/窗口并向其写入内容? [英] Open a new tab/window and write something to it?

查看:23
本文介绍了打开一个新选项卡/窗口并向其写入内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

但是我总是收到这个错误

However I always get this error

[异常...操作不安全."代码:18"nsresult:0x80530012(安全错误)"

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

有什么办法可以解决这个异常?

Is there any way to get through this exception?

推荐答案

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

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

不要写"到窗口,只用你需要的内容打开它:

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

参考:数据 URI

这篇关于打开一个新选项卡/窗口并向其写入内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆