1

I'm trying to do something like this

win = null;
win = window.open('/url/to/link','tab');
win.focus();

but in IE7 it returns me at the line of win.focus(); the error that win is null.

How can I solve it?

Thanks in advance!

Pointy
  • 405,095
  • 59
  • 585
  • 614
albertopriore
  • 614
  • 2
  • 9
  • 36
  • Does the window open? In any case that's not going to work for Firefox, Chrome, and Safari - they generally don't allow a window to force another window to get focus. – Pointy Oct 13 '10 at 11:59
  • Getting the error in Firefox too – Savage Nov 28 '18 at 19:41

3 Answers3

2

Blockquote < Return Value

Returns a reference to the new window object. Use this reference to access properties and methods on the new window.

Internet Explorer 7 on Windows Vista: Opening a new window from an application (other than the Internet Explorer process) may result in a null return value. This restriction occurs because Internet Explorer runs in protected mode by default. One facet of protected mode prevents applications from having privileged access to Internet Explorer when that access spans process boundaries. Opening a new window by using this method generates a new process. For more information about protected mode, see Understanding and Working in Protected Mode Internet Explorer. This commonly occurs for applications that host the WebBrowser control.> Window.Open method documentation

smirkingman
  • 6,167
  • 4
  • 34
  • 47
  • What is this `Blockquote < Return Value`? Please can you give more info. – Nishat Lakhani Jun 08 '16 at 10:06
  • That first line should read "Return Value". It is simply a reference to the paragraph halfway down the page that I linked to in the documentation, which explains why "Return Value" may be null. – smirkingman Jun 09 '16 at 07:10
2

You can try adding a slight delay to make sure that the window is open

//win = null;  <--useless
win = window.open('/url/to/link','tab');
if(win)window.focus();
else{
    var timer = window.setTimeout( function(){ if(win)win.focus(); }, 100 );
}

This day in age, most people avoid pop up windows and use modal layers.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • `This day in age, most people avoid pop up windows and use modal layers.` What if you wanna open an external link and you can't use an anchor tag for a reason? – Amin Jafari Jan 07 '19 at 13:27
0

When you launch a popup, give it a variable name:

myWin = window.open(etc)

//in the child window, call window.opener.myFocusFunction()
//in the parent window, use this...

function myFocusFunction(){
   myWin.focus();
   //myWin.blur();
   //uncomment as needed!
}

Have a play, it works for me.

tolli
  • 11