Search code examples
javascriptfirefox-addonfirefox-addon-restartless

XMLHttpRequest POST and open target page in new window/tab


How do you emulate Form's 'POST' action with target="_blank" in XMLHttpRequest? (ie post data and open target page in a new tab)


Solution

  • gBrowser offers this functionality right out of the box.

    var dataStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
    dataStream.data = "foo=bar&alpha=beta"; // make sure the values are properly encoded with encodeURIComponent
    
    var postStream = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
    postStream.addHeader("Content-Type", "application/x-www-form-urlencoded");
    postStream.addContentLength = true;
    postStream.setData(dataStream);
    
    gBrowser.loadOneTab("http://www.example.com/", {inBackground: false, postData: postStream});