2

How can I open a browser (i.e. Firefox, Chrome, IE, etc.) window from an XUL application?

I've tried the obvious window.open(url, "_blank") but the only effect this has is to close the current XUL window (!). A long trawl of Google hasn't produced anything fruitful either.

Just to make things clear, what I want is to open a URL in the user's default browser. I don't want to open the URL in my XUL application.

Oliver Moran
  • 5,137
  • 4
  • 31
  • 45

2 Answers2

1

Not sure if this helps you, but i use this to open a new tab.

var win = Components.classes['@mozilla.org/appshell/window-mediator;1']
    .getService(Components.interfaces.nsIWindowMediator)
    .getMostRecentWindow('navigator:browser');
win.gBrowser.selectedTab = win.gBrowser.addTab(myUrl);

You need to set myURL to the page you want to open.

More Information here

Also note that this will surely not open IE or Chrome. I'm happy with that as long as it does not open IE:-)

Community
  • 1
  • 1
mainguy
  • 8,283
  • 2
  • 31
  • 40
1

This is what I do:

function OpenInDefaultBrowser(uri)
{
    if(typeof uri == 'string')
    {
        var ioservice = Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
        uri = ioservice.newURI(uri, null, null);
    }

    // Open URL in user's default browser.
    var extps = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
        .getService(Components.interfaces.nsIExternalProtocolService);
    extps.loadURI(uri, null);
}
nicktook
  • 154
  • 1
  • 1
  • 8
  • +1 - This does the business but it nags for the user to select the appropriate application for the protocol (e.g. HTTP or HTPPS). Do you know of anyway around that? – Oliver Moran Feb 12 '14 at 15:44