IE7 window.open when .focus return null

断了今生、忘了曾经 提交于 2020-01-06 01:44:57

问题


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.



来源:https://stackoverflow.com/questions/3923321/ie7-window-open-when-focus-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!