javascriptfirefoxfirefox-addonxpcom

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


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

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

Is there any way to get through this exception?


Solution

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

    For reference: data URIs