views:

2337

answers:

5

I am creating a Firefox Extension...what would be the javascript to open a URL in the current tab from a menuitem?

e.g. in my overlay.xul file i have the following line:

<menuitem label="Visit homepage" oncommand="window.location='http://www.somepage.com'"/&gt;

but in firefox, when i click on the menu item, it opens the URL but it screws up the entire layout of the browser.

What's the correct way to do this?

+7  A: 

After browsing around, I found that I had to replace the above code with this:

<menuitem label="Visit homepage" oncommand="content.wrappedJSObject.location='http://www.somepage.com'"/&gt;
aryo
One thing I'd note is you might want to create a JavaScript function that handles this, just in case you want to use it for other click events throughout your extension!
Alex
A: 

hey friend you had provided the great code, i know you were get this by most efforts, but i get it quickly from you, so very thanks for this, its working great for me, i have to really keep you in contact, because i am also started to work on firefox extensions, so please send me your mail id. my id is : [email protected]

Thanks. Ashish

A: 

Call this JS functions on your commmand

//open a url current window:
function openUrl(url) {
content.wrappedJSObject.location = url;
newTabBrowser = gBrowser.selectedBrowser;
newTabBrowser.addEventListener("load", highlight, true);
}

//new tab
function openUrlNewTab(url) {
var win = Components.classes['@mozilla.org/appshell/window-mediator;1']
            .getService(Components.interfaces.nsIWindowMediator)
            .getMostRecentWindow('navigator:browser');
win.gBrowser.selectedTab = win.gBrowser.addTab(url);
}
+2  A: 

From a menuitem you can use openUILinkIn. It works like:

openUILinkIn(url, where);

where can be: tab, current, window (and a few other not as used options)

If you want to behave differently based on what keyboard modifiers a user is pressing, you can use another function whereToOpenLink, which returns tab/current/window based on the users preferences and modifiers.

openUILinkIn(url, whereToOpenLink(event));

Thus I use:

<menuitem label="Visit homepage" 
          oncommand="openUILinkIn('http://site.com', whereToOpenLink(event))"/>

If you aren't in the context of a menuitem you might want to check out another built-in XBL that adds linking and opening HREFs for a label:

<label value="google" class="text-link" href="http://google.com" />
anotherjesse
Where did you get the information this method from? I've been searching in vain for some actual documentation on what you can do with Javascript in a FF extension.
Zarkonnen
A: 

You are a freaking godsend !!!!!! Thanks a lot ... If you are a woman I could marry you right now.

Somebody who loves your code
this would be a comment, not an answer.. ;)
Tom Brito