Current URL in FireFox November 30, 2006Posted by xk0der in : Programming , 9comments
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’
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";
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
If they change the name, all you have to change is the first line, that’s it!
Hope this was helpful !
Like this post?