window.focus() 不适用于 setInterval()
我正在努力使代码正常工作...
然后,我想知道为什么 window.focus() 和 window.blur() 方法不起作用/只能通过输入按钮起作用。当我从 setInterval() 调用时它们不起作用。
例如,在下面的情况下,当我在 HTML 中按下按钮时,它第一次创建窗口,然后从我第二次按下按钮时聚焦窗口。
但是,我还设置了 setInterval() 来使窗口获得焦点,但它不起作用。我可以在控制台上看到日志,因此 setInterval 函数可以正常工作,但 win.focus() 会被忽略。
<head>
...
<script>
var win;
function makePopup(){
if (!win || win.closed) {
win = window.open("","","width=200, height=200");
win.blur();
}else{
win.focus();
console.log("opened");
}
}
setInterval(function(){makePopup();},4000);
</script>
</head>
<body>
<input type="button" onclick="makePopup()"/>
</body>
所以问题是:
- window.blur() 根本不起作用。
- window.focus() 仅当我从 HTML 按钮调用 makePopup() 时才起作用,而在 setInterval() 中不起作用。
我正在 Mac OSX 上的 Chrome 和 Safari 中进行测试。
如果您能给我一些建议,我真的很感激。
提前致谢。
I'm struggling to make the code blow work...
Then, I'm wondering why window.focus() and window.blur() methods are not working / only working from input button. They are not working when I call from setInterval().
For example, in the case below, when I push the button in HTML, it makes window at first time, and then focus the window from second time I press the button.
However, I also set setInterval() to make the window focus but it doesn't work. I can see the log on console, so setInterval function works correctly but win.focus() is ignored by somehow.
<head>
...
<script>
var win;
function makePopup(){
if (!win || win.closed) {
win = window.open("","","width=200, height=200");
win.blur();
}else{
win.focus();
console.log("opened");
}
}
setInterval(function(){makePopup();},4000);
</script>
</head>
<body>
<input type="button" onclick="makePopup()"/>
</body>
So the problems are:
- window.blur() doesn't work at all.
- window.focus() works only when I call makePopup() from the HTML button and doesn't work from setInterval().
I'm testing in Chrome and Safari with Mac OSX.
If you could give me some suggestion, I really appreciate it.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览器知道某人单击链接打开弹出窗口和何时由脚本触发弹出窗口之间的区别。为了防止恶意活动,许多浏览器会阻止脚本弹出窗口。
通过指定与托管页面位于同一域的 URL,它应该可以工作。
The browser knows the difference between someone clicking a link to open a pop-up and when it is triggered by a script. In order to prevent malicious activity many browsers prevent scripted pop-ups.
By specifying a URL that is on the same domain as the hosted page, it should work.