views:

370

answers:

1

Hi All, Can anybody tell me how to get the URl of the next tab on firefox ? I am using this now :

//The browser object points to the new tab which I capture using the
//'TabOpen' Event
var browser = gBrowser.getBrowserForTab(event.target);

//From where can I get the URL of this new tab ? Also, how to I get
//the Title of this new Tab

Thanks in advance..

+5  A: 

Let's try to figure this out together. First search google for "getBrowserForTab" to see what kind of object it returns. You'll see a page with examples as the first hit, and the reference page as the second hit. The latter is what we're looking for. It says:

[getBrowserForTab( tab )] Returns a browser for the specified tab element.

Follow the link for browser to see what properties and methods this object has.

You'll see it has a contentTitle property ("This read-only property contains the title of the document object in the browser."), which answers the second part of your question.

Similarly you see it has a currentURI property which returns "the currently loaded URL". The returned object is an nsIURI, to get its string representation you need to use currentURI.spec, as described in the nsIURI documentation.

So to sum up:

var title = browser.contentTitle; // returns the title of the currently loaded page
var url = browser.currentURI.spec; // returns the currently loaded URL as string

You could also just get the window/document objects of the content page via browser.contentWindow/browser.contentDocument and get the title/URL (and other things) using the APIs you would use in a regular web page.

Hope this helps and you'll try to do this yourself next time you ask a question (and if you can't find the documentation or have trouble understanding it, indicate what specific problems you encountered).

Nickolay
wow.. Thank you so much. :)
coder