views:

2099

answers:

3

I'm trying to open a new browser tab with the results of a POST request. I'm trying to do so using a function containing the following code:

var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"]
    .getService(Components.interface
s.nsIWindowMediator);
var browserWindow = windowManager.getMostRecentWindow("navigator:browser");
var browser = browserWindow.getBrowser();
if(browser.mCurrentBrowser.currentURI.spec == "about:blank")
    browserWindow.loadURI(url, null, postData, false);
else
    browser.loadOneTab(url, null, null, postData, false, false);

I'm using a string as url, and JSON data as postData. Is there something I'm doing wrong?

What happens, is a new tab is created, the location shows the URL I want to post to, but the document is blank. The Back, Forward, and Reload buttons are all grayed out on the browser. It seems like it did everything except executed the POST. If I leave the postData parameter off, then it properly runs a GET.

Build identifier: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1

Thanks in advance.

A: 

try with addTab instead of loadOneTab, and remove the last parameter.

Check out this page over at the Mozilla Development Center for information on how to open tabs.

You could use this function, for example:

function openAndReuseOneTabPerURL(url) {
  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                     .getService(Components.interfaces.nsIWindowMediator);
  var browserEnumerator = wm.getEnumerator("navigator:browser");

  // Check each browser instance for our URL
  var found = false;
  while (!found && browserEnumerator.hasMoreElements()) {
    var browserInstance = browserEnumerator.getNext().getBrowser();

    // Check each tab of this browser instance
    var numTabs = browserInstance.tabContainer.childNodes.length;
    for(var index=0; index<numTabs; index++) {
      var currentBrowser = browserInstance.getBrowserAtIndex(index);
      if ("about:blank" == currentBrowser.currentURI.spec) {

        // The URL is already opened. Select this tab.
        browserInstance.selectedTab = browserInstance.tabContainer.childNodes[index];

        // Focus *this* browser
        browserInstance.focus();
        found = true;
        break;
      }
    }
  }

  // Our URL isn't open. Open it now.
  if (!found) {
    var recentWindow = wm.getMostRecentWindow("navigator:browser");
    if (recentWindow) {
      // Use an existing browser window
      recentWindow.delayedOpenTab(url, null, null, null, null);
    }
    else {
      // No browser windows are open, so open a new one.
      window.open(url);
    }
  }
}
Marius
A: 

Something which is less Mozilla specific and should work reasonably well with most of the browsers:

  • Create a hidden form with the fields set up the way you need them
  • Make sure that the "target" attribute of the form is set to "_BLANK"
  • Submit the form programatically
Cd-MaN
+2  A: 

The answer to this was found by shog9. The postData parameter needs to be a nsIMIMEInputStream object as detailed in

http://developer.mozilla.org/index.php?title=En/Code_snippets/Post_data_to_window

aryeh