Current URL in FireFox

I required this while creating an extension for Firefox that bookmarked the current URL at some site.

After some hacking I found that the Address bar object that displays the current tab’s URL has the id set to ‘urlbar’

Following simple Javascript code accomplishes the task


var obj = document.getElementById("urlbar");

returns the reference of the address bar. Now you may use obj.value to get the URL text in the address bar.

There is a function implemented by FireFox named handleURLBarCommand(); that opens the current URL in the address bar in the current tab.

So the following code will open http://ensparc.com in the current tab.


var ub = getElementById("urlbar");
ub.value = "http://ensparc.com";
handleURLBarCommand();

Using handleURLBarCommand() function may not be the standard function to open URLs but Mozilla developer probably won’t rename it to anything else in the near future.

if you want to safeguard yourself from probable future modifications use the following method


var myHandleURL = handleURLBarCommand
// Now use myhandleURL wherever you
// were to use handleURLBarCommand
myHandleURL();

If they change the name, all you have to change is the first line, that’s it!

Hope this was helpful !

9 Comments on “Current URL in FireFox

  1. Hey, I was wondering if you might be able to help me with a little scripting. What I wanna do is to get the current url and compare it with a specified string. If the comparison turns out to be true, I want to remove the url, i.e. replace it with “” so to say. For testing purposes I first want to just replace the url with any tesxt. I guess getting the url itself isn’t much of a problem. I’m now using your method from above for this (I used document.getElementById(“urlbar”).getAttute(“value”) before). With a little help of the DOM Inspector I wrote this script for my userChrome.js which just doesn’t work. You don’t have an idea what I might be doing wrong, do you?

    (function()
    {
    var obj = document.getElementById(“urlbar”);
    if(obj.value == “file:///home/nik/.startpage/home.htm”)
    {
    document.getElementById(“urlbar”).setAttute(“value”, “bra”);
    }
    })();

  2. @Nik : I don’t know why you would like to do such a thing. But anyways, the mistake lies on the line within the if() block.

    documen.getElementById(“urlbar”).setAttribute(“value”, “bar”);

    should read.

    if(…)
    {
    obj.value = “My value”; // Since obj already holds reference to urlbar
    }

    OR

    if(…)
    {
    document.getElementById(“urlbar”).value = “My value”;
    }

    using setAttribute will change the attribute’s value, but not the “value” property, which is the actual text that is displayed.

    As demonstrated in the post, to read and write the text displayed, use the property named “value” not the attribute named “value”.

  3. First of all, thanks for your quick reply. I didn’t know obj served as some kind of a pointer so to say. I’m not familiar with javascript at all, so bear with me. However, it still doesn’t work with the changes I made now. I hope I haven’t forgotten to change something, rendering my reply pointless.

    (function() {
    var obj = document.getElementById(“urlbar”);
    if(obj.value == “file:///D:/Modding/startpage/home.htm”) {
    obj.value = “brap”;
    }
    })();

    The reason for the removal is a purely aesthetic one. I’m currently heavily modding my entire desktop, from gtk theme to firefox layout. I’m also using a custom start page and it simply looks ugly to me to have the file url shown. That’s why I only need to remove it for a certain string.

  4. When are you calling your function? I guess you haven’t tied it to any event.

    You probably need to do something like this:
    /**/
    function myHandler () {
    var obj = document.getElementById(”urlbar”);
    if(obj.value == “file:///D:/Modding/startpage/home.htm”) {
    obj.value = “brap”;
    }
    }

    gBrowser.addEventHandler(“TabSelect”, myHandler, true);
    /**/

    So your function ‘myHandler’ would get fired up every-time the user selects a Tab. You probably want to tie your handler to more events than just the TabSelect event. This may help: https://developer.mozilla.org/en/Code_snippets/Tabbed_browser

  5. Ugh, you see I don’t really know what I’m doing here which doesn’t make it easy. I didn’t have a handler. I sort of based my script on another one I found online that gathered all menus, i.e. File, Edit, … in one. A compact menu so to say. And that script didn’t use a handler either. The menu was permanently replaced. Might be cause it’s sort of a static thing. The menu doesn’t change, unlike the url which is why you need a handler. Makes perfect sense.

    Unfortunately it still doesn’t work with the lines you posted above. I’m really sorry for taking up your time with this issue, but I really don’t know where else I could get help from. I already posted on two boards about it, but I guess the issue is a little too abstract for people to take interest in it :/

  6. I did it! Or rather, you did. addEventHandler had to be addEventListener. That did the trick. Man, thank you so much. You wouldn’t believe how much time I’ve already spent figuring this thing out. Now the only thing left for me to find out is how I can do the same thing with tab labels. The “(Untitled)” ain’t too pretty either and var obj = document.getElementById(“tabbrowser-tab”); doesn’t do the trick yet. Sorry for leaving such a mess in your comments btw. Feel free to remove them if you like. Again, thank you very much! You’ve been of great help.

  7. @nik: My bad 🙂 .. I had read it twice and that thing appeared to me as “addEventListener” instead of addEventHandler :), maybe coz it was midnight at my place 😀

    Good to hear that you got it working. As for the tab labels, try this as a starting point:

    Put the following line inside the if() block in the myHandler() function.

    if(..)
    {
    ….
    gBrowser.selectedTab.label = “My New Label”;
    ….
    }

    Look here for more information:
    https://developer.mozilla.org/en/XUL/tabbrowser

  8. Worked like a charm. You’re my hero. Thank you very much. I’ll definitely recommend your blog :p Farewell!

Leave a Reply

Your email address will not be published. Required fields are marked *