3

I'm trying to get the window.focus() function to work with no luck.

Take a look at this fiddle

var myWindow = window.open('','zzz','width=600,height=700');
    myWindow.document.write('test');
    myWindow.focus();​

If you click run after the jsfiddle page loads then the new window should get back focus. What am I doing wrong?

Charles
  • 50,943
  • 13
  • 104
  • 142
qwertymk
  • 34,200
  • 28
  • 121
  • 184

3 Answers3

1

It "works" for me in FF 15. Users can disable the ability of scripts to open and focus windows, check your settings. Oh, and the pop–up should get focus by default, so you shouldn't have to call myWindow.focus().

Some minor points that probably having nothing to do with the issue but you may want to fix:

  1. A valid document should be written to the new window, a title element and one block element are required, e.g. document.write('<title></title><div></div>, a DOCTYPE is highly recommended too
  2. The input stream should be closed after you've finished writing, use document.close()
RobG
  • 142,382
  • 31
  • 172
  • 209
0

For Chrome at least (dont have FF) just replace

myWindow.focus();

with

myWindow.blur();
setTimeout(myWindow.focus, 0);

EDIT: Realized I've got FF in a linux VM. The code is fine with the current Chrome and with FF12 under the newest Mint x64

enhzflep
  • 12,927
  • 2
  • 32
  • 51
0

This works for me:

<script> 
var popupWin;
function open_popup(url) {
    if(typeof(popupWin) == "object" ) popupWin.close();

    popupWin =  window.open(url, 'PopupName', 'scrollbars=no,resizable=yes, width=600,height=800,status=no,location=no,toolbar=no'); 
    popupWin.focus();

 }
</script>
Burt
  • 56
  • 3