Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus tab or window

Tags:

for a little app, I'm opening a few windows/tabs from my script. Whether the browser opens a window or a tab is of course not in my hand.

However, I hold the references to the newly created window objects and I do change their content "remotely" from another window. This all happens under the same document.domain so no xss problem.

The problem is, I cannot reliably focus those created windows/tabs. Since I'm writing a very specific app for a customer, I'm only targeting Firefox as browser. One option I have is of course just to do a remoteWindow.alert('foobar'); to get bring that window/tab up front, but that is pretty ugly isn't it.

I found this answer How to focus window/tab like alert()?

and it's said there, that Firefox has an option to allow script focus. So finally my question is, what is that option ? I searched the about:config for "tabs" and "focus" but didn't find anything related.

How to configure ?

like image 326
jAndy Avatar asked Nov 15 '11 10:11

jAndy


People also ask

What does window focus mean?

Window focus() The focus() method sets focus to a window. The blur() method removes focus from a window.

What is difference between window and tab?

A tab is more or less same as a window. A window can contain several tabs and all session data and cookies are shared across all tabs and open window. It's better to open a lot of tabs than opening several windows because too many window becomes too cluttered to handle.

How do I know if my browser tab is in focus?

Use the visibilitychange event to detect if a browser tab has focus or not, e.g. document. addEventListener('visibilitychange', checkTabFocused) . The event is fired at the document when the contents of its tab have become visible or have been hidden.


2 Answers

The following appears to work in IE8 and FF13:

<script type="text/javascript"> // Stupid script to force focus to an existing tab when the link is clicked. // And yes, we do need to open it twice. function openHelp(a) {     var tab = window.open(a.href, a.target);     tab.close();     tab = window.open(a.href, a.target);     return false; } </script> <a href="help.html" target="help" onclick="return openHelp(this);">Help</a> 
like image 134
Alex Avatar answered Feb 15 '23 22:02

Alex


The only solution I see, is to force the popup in a new window, since there doesn't seem to be a way to focus another tab. This solution also requires you to change the default Javascript security settings in Tools > Options > Content tab and click on the Advanced button next to Enable Javascript checkbox and check the middle box to allow focusing windows.

To force the use of a window rather than a tab, use win = window.open("http://www.google.com", "test" ,"modal=yes"); and then call win.focus(); whenever you feel like it.

EDIT: Actually forgot to mention the fact that this is FF only.

like image 45
zatatatata Avatar answered Feb 15 '23 22:02

zatatatata