47

I have a link which should open in a new tab, but if the tab is already open, just switch to it. I've tried with javascript, wnd = window.open() and than wnd.focus(), that works in Chrome 19, but not in FF 13 or IE 9. Here's the code I've written :

<script type="text/javascript">
var loadingTableWnd;
function openOrSwitchToWindow(url){
if(loadingTableWnd == undefined)
loadingTableWnd = window.open(url,'myFrame');
else
loadingTableWnd.focus();
}
</script> 
<a href='javascript:openOrSwitchToWindow("/");' >Loading Table</a>

Any idea how can I open or switch to from every browser?

EDIT: I need to open the link in a new tab, not a stand-alone window.

jaraics
  • 4,239
  • 3
  • 30
  • 35
  • There is a bug that has been filed a while ago, hasn't been fixed yet - https://bugzilla.mozilla.org/show_bug.cgi?id=416771 – Yogesh Mangaj Dec 24 '15 at 10:11
  • That bug was submitted in 2008. Here it is in 2016 with Firefox 47 and it still has not been resolved. Can anyone (with some pull) please escalate? – Lonnie Best Jul 27 '16 at 19:44
  • In 2019 (Firefox 69) the bug was fixed. – Chris May 14 '20 at 21:44
  • If the url opens in a new window or tab depends on browser settings. To open in a new window instead of a tab I use window [specs](https://www.w3schools.com/jsref/met_win_open.asp): `let windowFeatures = 'resizable=yes'; window.open('url', 'whatever', windowFeatures);`. Works in Chrome. – Felix Sep 09 '22 at 06:33

6 Answers6

46

Different browsers behave differently for window.open() and focus(). For this code window.open('www.sample.com','mywindow').focus()

  • Chrome 20 opens a new tab, and focuses on subsequent open() calls regardless if focus() is called or not.
  • Firefox 13 opens a new tab, focuses on first open(), does not focus on subsequent open() calls/disregards focus().
  • IE 8 opens a new window, honors focus().
  • Safari 5 opens a new window, and focuses on subsequent open() calls regardless if focus() is called or not.

Fiddle to test with: http://jsfiddle.net/jaraics/pEG3j/

jaraics
  • 4,239
  • 3
  • 30
  • 35
  • 2
    If possible, please update your answer for iOs 9.3.2 - iphone6 (does not matter Chrome or Safari) : opens a new tab, focuses on first open(), does not focus on subsequent open() calls/disregards focus(). – Phung D. An Jul 24 '16 at 23:26
  • @PhungD.An see [here](https://stackoverflow.com/a/73690887/4420532) – Felix Sep 12 '22 at 14:31
  • How do I write a unit test for this in jest framework? Anyone any idea? – Naga Apr 26 '23 at 21:03
21

You shouldn't need any logic for something like this. By default, specifying the second parameter for window.open() gives the window a "name", that the browser remembers. If you try to call window.open() with the same name (after it's already been opened), it doesn't open a new window...but you might still need to call .focus() on it. Try this:

var a = window.open(url, "name");
a.focus();

Those should be the only lines of code in your function, and you don't need the loadingTableWnd variable...

Ian
  • 50,146
  • 13
  • 101
  • 111
  • 1
    Giving a name indeed prevents opening a new window. But my problem is, that the user can navigate away(some functionality is controlled with hash(#) in the url) and I don't wish to reset that. I need the variable, because i must access the window from subsequent clicks on the link. Hence the need of a global variable. – jaraics Jun 13 '12 at 06:35
  • @Ian Not true with latest version of windows OS like windows server 2012 R2 – Gandhi Oct 11 '17 at 07:47
10

If the window is already opened and if you want to focus on that window you can use

window.open('', 'NameOfTheOpenedWindow').focus();
dileepar
  • 171
  • 2
  • 5
3

window.focus() is widely supported and seems to be working fine in both Internet Explorer and Firefox for me, the problem should be in your code. I've created a simple jsFiddle for you to test.

Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
Sergey Rybalkin
  • 3,004
  • 22
  • 26
  • Thanks for the jsfiddle, but I can't get it to work with tabs(see my edit). Also I need to open a page on the current domain. (btw, your fiddle got removed for some reason) – jaraics Jun 13 '12 at 06:30
2

If you are not interested in retaining the state of a previously opened tab, you can do this:

var loadingTableWnd;
function openOrSwitchToWindow(url) {
    if (loadingTableWnd != undefined) {
        loadingTableWnd.close();
    }
    loadingTableWnd = window.open(url,'myFrame');
}
MarkZ
  • 21
  • 1
1

It will always redirect to new tab with focus:

const redirectBlank = function(url) {
  return window.open(url, Date.now()).focus();
};
Gigoland
  • 1,287
  • 13
  • 10