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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.