prabhakaran
prabhakaran

Reputation: 5274

Browser Extension - How to get the current page's URL

I am making a custom button. When I click that,it should show the "current page's URL". I found the answer as "document.location" or "windows.location" . But, both points to the local XUL location "chrome://browser/content/browser.xul" not the original URL. Can anybody show how to accomplish this?

Upvotes: 1

Views: 1585

Answers (1)

Ajai
Ajai

Reputation: 3500

Try anyone of these.. One should definitely work,

window.top.getBrowser().selectedBrowser.contentWindow.location.href;

window.content.location.href

function getURL{

    var currentWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");

    var currBrowseSession = currentWindow.getBrowser();
    var currURL = currBrowseSession.currentURI.spec;

    return currURL;
}

Upvotes: 3

Related Questions