[Javascript] Open Mozilla or Firefox tabs?

Hakan Magnusson (Backbase) hakan at backbase.com
Tue Aug 10 08:09:23 CDT 2004


>  Tough I found that you may middle button click the links, or set in 
> your preferences to always open new windows on tabs.

There is a way to open a new tab through scripting in Mozilla, or at 
least I would be willing to bet my mortgage on it, my credit history 
permitted. The Web Developer Toolbar extension for Firefox open new 
pages in tabs (for validation, among other things) and AFAIK it's all XUL.

I don't know and I don't have the time right now to figure out exactly 
how this can be done, but I'm guessing that rather than specifying 
target="_tab" or so you would need to first create a new tab (through 
JavaScript) and then set the location of that tab to your new page.

Now, assuming that someone figures this tab-detail out, the only thing 
left would be to implement it in some degradable manner. Something like 
this should be executed when the page has finished loading:

---
var aLinks = document.getElementsByTagName('a');
for(var j=0; j < aLinks.length; aLinks++) {
	if(aLinks[j].getAttribute('target') == '_blank') {
		// replace links that open in a new window
		// so that they open in new tabs
		aLinks[j].onclick = function() {
			var sHref = this.getAttribute('href');
			var oTab = new Tab(); // Or whatever...
			oTab.location = sHref;
			return false;		
		}
	}
}
---

In theory, this will run over all <a>-tags, check if they are targeted 
to open in a new window and if so, applies a custom onclick event that 
creates a new tab and sets the location. The function then returns 
'false', blocking the link from being fired.

If you DO find a method to scriptically create tabs please, please post 
it here. :)

Regards,
H




More information about the Javascript mailing list