In Javascript, we can use window.open() to open a new browser window or tab. But if a tab is already open, it should highlight that only. It should not open duplicate tabs. How to do that?
-
possible duplicate of [Check if window is already open window.open](https://stackoverflow.com/questions/12138236/check-if-window-is-already-open-window-open) – Anderson Green Mar 15 '22 at 14:53
4 Answers
The second argument of window.open(strUrl, strWindowName[, strWindowFeatures]);
is the window name. if you specify that parameter, to anything other than "_blank" it will refer to the already opened tab/window.
For instance:
window.open('/about', 'newwindow');
and
window.open('/contact', 'newwindow');
will open the page in an already opened window/tab.

- 30,469
- 8
- 53
- 60
-
ya.. i did that... it worked.. but is there any way that it wont reload that page if it is already open? i just want to focus that... – Shashwat Feb 29 '12 at 11:04
-
You could try `newwin = window.open(...')` and `newwin.focus();` It works for popups but not tabs in firefox. In IE I think it defaults to opening new windows so it works. – Candide Feb 29 '12 at 13:09
-
I already did that. It is focusing but still it is reloading the window already present. Is there any way to avoid that? – Shashwat Mar 01 '12 at 03:58
-
you could do this `if (newwin != null) { newwin = window.open('...'); } else { newwin.focus(); }` – Candide Mar 01 '12 at 15:04
Make sure you supply the same window name to window.open() every time! (second parameter, must not be empty)
You will need to manage the window object returned from window.open() and check if it was closed or not, check out https://developer.mozilla.org/en/DOM/window and the closed
property. You will have to have a window to url list which will help you decide if to use window.open() to open a new window (a url which isn't open at the moment) or use the openedWindow.focus() (openedWindow is the object returned by the previous call to window.open()) to bring the window into view.

- 1,595
- 9
- 14
-
ya.. i did that... it worked.. but is there any way that it wont reload that page if it is already open? i just want to focus on that... – Shashwat Feb 29 '12 at 10:13
Give the window a target name: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
New urls wil open in it if exist already

- 320
- 1
- 8
Candide's code needs a slight correction. The null condition should be checked and not "!="
popwin.html is the page you want to open in a popup. I know this is already answered, just posting in case someone wanted to refer later on.
MAIN WINDOW CODE:
<script type='text/javascript'>
var newwin;
function popup(){
if (newwin == null)
{
window.open('popwin.html', 'newwin');
}
else
{
newwin.focus();
}
}
</script>
CALL THE POPUP in body area of same page:
<a href="#" onClick="popup();">Open Window</a>

- 93
- 1
- 13