views:

1263

answers:

3

I've written a bookmarklet to look a word up in a Chinese dictionary:

javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('%E8%AF%8D%E8%AF%AD',''))};if(Qr)(function(){window.open('http://nciku.com/search/all/'+Qr);})();

This opens a new tab with search results for your selected word or a word you type in at the prompt. Is there a way to load the new tab in the background? I'd like to keep the focus on the page I'm looking at, and look at the search results later.

There is an option "When I open a link in a new tab, switch to it immediately" in Firefox, this doesn't help.

Edit: Note that this is for my use, so an answer that tells me how to change Firefox (3.0.11) settings to do this would work as well. Also I've tried the following modification, but it's still focusing the new tab.

javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('%E8%AF%8D%E8%AF%AD',''))};if(Qr)(function(){var%20oldWin=this;window.open('http://nciku.com/search/all/'+Qr);oldWin.focus()})();

Edit 2:

Looking around to see if I can find an answer I see this guy who's got the opposite problem (new tabs don't get focus, but he wants them to have it), but with no resolution:

http://stackoverflow.com/questions/72943/possible-to-set-tab-focus-in-ie7-from-javascript

There's apparently talk about a _tab target in HTML 5, but that doesn't help me much.

http:/ /forums.whatwg.org/viewtopic.php?t=185&highlight=tab+focus

(apparently as a new user I can only post one link, so I've mauled it)

This seems pretty broken browser behaviour if this is impossible.

A: 

Apparently this is only possible with previously opened windows, not the root window.

Calls to window.open with the same window name as an already existing window, loads the URL into that window and gives a reference to the window back. The window isn't given focus, its opener property isn't changed, and a third argument to window.open is ignored. You can use the focus method to give the window focus manually.

var oldWin = window.open("url.html","oldName"); 
oldWin.focus(); // give focus
Luca Matteis
Adding window.focus(); at the end doesn't seem to have any effect. Is this how I should use it?
Simon D
I added some code, try that.
Luca Matteis
Changing the bit at the end to: if(Qr)(function(){var%20oldWin=window.open('http://nciku.com/search/all/'+Qr);oldWin.focus()})(); still opens the new tab in the foreground. I also tried adding this, with no success: if(Qr)(function(){var%20oldWin=this;window.open('http://nciku.com/search/all/'+Qr);oldWin.focus()})();
Simon D
Does that mean there's no way to do this? It sounds like quite a basic thing to me :(
Simon D
Yes, there's no way of doing this. Maybe IE6 supports it, but not modern browsers.
Luca Matteis
Adding a second parameter to window.open of e.g. "openedWindow" will recycle the tab in the background. I.e. the first time you run it a new tab will open, after that it will be loaded in the background. Thanks for your help, I'll leave this open for now in case anyone else has some bright ideas (maybe an addon although that seems like overkill for something that should be simple).
Simon D
+1  A: 

No, not programmatically through JavaScript. You don't have control over the user's browser preferences, only they have control over that.

Moreover, even if you did have control over that, you shouldn't do it, because it undermines the control that your script is given to you by the browser. If the user wants a page to open in the background, they should be able to control it, not you, as the developer.

Perspx
In this case user==developer==me. If there's a way to set my browser preferences to allow me to do this, that would be a good answer too, but I can't find one.
Simon D
+1  A: 

In FireFox type about:config and change browser.tabs.loadDivertedInBackground to true. This has worked for me with browser bookmarklets.

source: http://lifehacker.com/263940/force-links-to-open-in-the-background

Thanks very much - that works perfectly.
Simon D