简体   繁体   English

window.focus()在Chrome中不起作用

[英]window.focus() is not working in Chrome

Here is my code: 这是我的代码:

var myWindow = window.open('','zzz','width=600,height=700');
myWindow.document.write('test');
myWindow.blur();
myWindow.focus();
window.onfocus=function(){
    this.document.write('<br />Focused!');
}
var w = window;
myWindow.onfocus=function(){
    this.blur();
    this.document.write('<br />Focusing on main window!');
    w.focus();
};
myWindow.focus();

Here is the fiddle: http://jsfiddle.net/maniator/nMGNS/ 这是小提琴: http : //jsfiddle.net/maniator/nMGNS/

When the popup window is focused it should print Focusing on main window! 当弹出窗口聚焦后 ,应Focusing on main window!打印Focusing on main window! on the popup and then focus on the main window and print Focused! 在弹出窗口上,然后将焦点放在主窗口上并打印Focused! over there. 在那边。

But it does not. 但事实并非如此。

Here is the information about my browser: 以下是有关我的浏览器的信息:

Google Chrome   22.0.1229.79 (Official Build 158531) m
OS  Windows
WebKit  537.4 (@129177)
JavaScript  V8 3.12.19.11
Flash   11.3.31.331
User Agent  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4

Aside : 除了

I know about the alert(...) hack, and I want to avoid it. 我知道关于alert(...) hack的信息,我想避免这种情况。

By @epascarello's comment,Changing document.write to alert ,It can be work in chrome: 通过@epascarello的注释,将document.write更改为alert ,可以在chrome中使用:

var myWindow = window.open('','zzz','width=600,height=700');
myWindow.document.write('test');
myWindow.blur();
window.onfocus=function(){
    alert('Focused!');
}
var w = window;
myWindow.onfocus=function(){
    this.blur();
    alert('Focusing on main window!');
    w.focus();
};
myWindow.focus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM