IE7 window.open when .focus return null

2019-07-30 04:32发布

问题:

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!

回答1:

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.



回答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



回答3:

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.