How to get window.focus() to work in firefox [duplicate] - javascript

This question already has answers here:
window.focus(), self.focus() not working in firefox
(3 answers)
Closed 9 years ago.
Does anyone have a clue on how to be able to use window.focus() in FireFox? (27.0.1).
I have tried all the about:config settings out there but still no luck.
My code looks something like this:
var wi = window.open('myUrl');
wi.focus();
== EDIT ==
I'm trying to make this work in FireFox on my machine, not for any other users so please
refrain from the standard "let your users decide" or "it depends on the user's settings" as
I want to get this to work for a local project :)

Tools > Options, Content area, "Advanced" button to the right of "Enable JavaScript" should give you the option to allow this behavior. If you're on Linux, the navigation might be slightly different.
Note, it is generally considered best practice to allow the user to choose what window should be in focus.
EDIT:
This is about:config you're referring to: dom.disable_window_flip
In FF 27.0.1 I am able to open a new window with focus by providing window name and option parameters to window.open, and then calling focus.
var wi = window.open('http://www.google.com', 'window_name', 'height=200,width=200');
wi.focus();
Example here: http://jsfiddle.net/CLVh2/
For Chrome support, you'll want to check out: Google Chrome "window.open" workaround?
EDIT: FOR TAB SUPPORT
If you want to open a tab and give the tab focus in FF (27.0.1), and simply calling window.open('url') is not giving you the desired result, try checking the "When I open a new tab, switch to it immediately" option. It can be found in Tools > Options > Tabs

According to Window.focus specification from Mozilla, that's not possible. It depends on the user's settings.
Makes a request to bring the window to the front. It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.

Related

how return focus on parent window using javascript?

is it possible return focus on tab parent using javascript?
I read some threads about this problem, but i didn't find solutions.
I tried
window.opener.parent.focus()
and
window.opener.focus();
but it doesn't work.
Can someone help me?
Thanks
Generally, you cannot do this inside a web page. Because it's the user's choice which tab/window she wants to focus on and browsers such as firefox and chrome respect such choices by providing configs to open new tabs in the background or not. But under several very special cases, you may still achieve this.
If you want to open a new tab and return focus immediately, you can try to simulate a 'ctrl+click' event on a link to open the tab on the background. Refer to this thread Open a new tab in the background?(Only for chrome, API may already changed. So it may only works on an obsoleted version)
If you are shipping with an extension, do it in the extension code. For example: in chrome extension.
If your script is for a customized browsers which you have control on / you can affect the design, you can implement the function in the browser side and expose an API for your script.

Chrome Opening Tab With Window.Open

Have looked at the answers to previous questions and am not sure if it
is the same issue as I am having. Some of the possible answers I have found
on the net are aged so am not sure whether anything has changed regards this.
I have this code to check for a cookie and then either open one or two
tabs dependent on whether a cookie is found.
On the html side I have a link like this:
<a target='_blank' onclick="newTab()" href="http://www.test.com">
In the head section the newTab() function does the cookie check then
if needed opens a window like this:
window.open('http://www.test.com/members/','_blank');
When the link is clicked the above works fine in Firefox and IE and both
windows open when needed but in Chrome only the onclick tab opens and not
the main url regardless of the cookie.
I imagine this is an issue relating to opening tabs/windows in Chrome but
I thought that the above would be dealt with by Chrome as the opening windows
are the result of a user click.
Am I missing something simple or is it a restriction on Chrome?
Thanks for any advice.
Indeed as answered by CBroe the reason for the problems was because
I had used _blank as the window name for both.
I changed one over to _parent and it is at least working like this now
so thanks for the assistance.

Javascript to open URL within a new Tab (instead of a window)

Hey guys. There are a few entries here requiring solutions to do the opposite of this, and others vaguely related. In one of them, the poster asked how to do this on Mozilla Firefox. Actually though, firefox will always open the URL in a new tab when window.open() is called, unless you set the window's size within its parameters.
So Mozilla and Chrome do what I want by default. The question is: how do I get Internet Explorer to open the URL I want within a new tab, as opposed to doing it in a new window?
Thanks in advance.
The obvious answer is to open the link with target="_blank". As you said, Firefox and Chrome will open a new tab.
Regarding IE - the behavior is up to browser preferences. By default (in IE7+ obviously) I believe the behavior is defined as open new tab. If the user decided the behavior should be a new window, there's only so much you can do about it.

Open a chrome tab from delayed javascript window.open

When I window.open("http://blarg") in chrome, I get a new tab. If I delay the open, say using a jquery $(hrm).animate({},5e3,function(){window.open(url)); it opens the url in a new window with no status bar, etc — if I give it permission to pop-up that is.
I'm looking for a way to get the instant behavior, that is, I wish to open a URL after an animation, but still in a new tab.
I imagine I could get by with learning a way to instruct chrome to never ever open pop-ups and to always open them in tabs (I imagine there's a webkit setting, why it's not a built in is a mystery); but I'd rather try to find a way to do it from the javascript if possible.
I somewhat doubt there's any way to do this though. I'm not aware of any javascript that's tab-aware.
A similar question was asked about tabs in Firefox, but the same answer applies:
There is no way to force a window to open as a tab. It's all dependent on the user's preference settings.

Firefox javascript bookmarklet open tab in background

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

Categories

Resources