admin管理员组

文章数量:1426087

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('/'+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('/'+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:

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/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.

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./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./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:

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/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.

Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Jun 28, 2009 at 20:25 Simon DSimon D 4,3105 gold badges42 silver badges47 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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

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

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.

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 

Facing the same issue, I only noticed that if you alert() something just after opening the window, Firefox would not switch to the newly opening tab.

本文标签: Firefox javascript bookmarklet open tab in backgroundStack Overflow