Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change browser focus from one tab to another

I have a JavaScript chat client running in one browser tab (call it tab A). Now when a message arrives the user might be on another browser tab (call it tab B). I'm looking for ways to change the focus from tab B to my chat client (tab A) when such a message arrives.

I could not find a way to do this.

like image 879
Max Avatar asked Apr 24 '10 11:04

Max


People also ask

How do I focus a tab in Chrome?

Focus Mode in Chrome Make sure you run Google Chrome Canary and that the browser is up to date. Load chrome://flags/#focus-mode. Set the flag to Enabled. Restart Google Chrome.

How do you switch tabs in Javascript?

Using Javascript, triggering an alert can have the desired effect. Run this code in your console, or add to your html file in one tab and switch to another tab in the same browser. setTimeout(function(){ alert("Switched tabs"); }, 5000); The alert appearing after the timeout will trigger tab switch.


2 Answers

It is not possible - due to security concerns.

unless by "tab" you mean a window and a popup window that (due to browser preferences) opened up in a new tab. If this is the case, then yes you can.

//focus opener... from popup window.opener.focus();  //focus popup... from opener yourPopupName.focus();  
like image 110
scunliffe Avatar answered Sep 23 '22 07:09

scunliffe


It is possible to shift focus back to Tab A by means of an alert in Tab A e.g. alert('New Message')

However, you need to be careful using this as it is very likely to annoy people. You should only use it if you make it optional in your app. Otherwise, updating Tab A's title and/or the favicon would appear to be best as nc3b says.

like image 29
Autoabacus Avatar answered Sep 23 '22 07:09

Autoabacus