Jump to content

Shifting focus to a browser tab


jhsachs

Recommended Posts

Here's a small JavaScript puzzle, probably trivial to those more familiar with JavaScript than I am.

 

My PHP script wants to open a new browser tab, display a page in it, and shift the focus to it. If the tab is already open, it wants to shift the focus to the tab without opening a new one.

 

I found that window.open does what I want, as far as opening the tab goes, out of the box. Thus I can open the tab if and only if it doesn't already exist like this:

 

window.open('www.somewhere.com/newtab_page.html', '_newtab');

 

window.open returns a reference to the tab whether it already exists or not, right? So I should be able to open the tab if it doesn't exist and make it the current tab like this:

 

var newtab = window.open('www.somewhere.com/newtab_page.html', '_newtab');
newtab.focus();

 

But it works only if the tab does not already exist. If the tab exists, it does nothing. Why is that, and how can I fix it?

Link to comment
https://forums.phpfreaks.com/topic/244279-shifting-focus-to-a-browser-tab/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.