javascript setTimeout to window.open 和 close,在同一个窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30144669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setTimeout to window.open and close, on the same window?
提问by biscuit_
I'm having a little difficulty opening up windows after a period of time, and then closing them after a period of time, automatically. I'm not sure why, but it seems like when I try to use setTimeout on a window.open and window.close they interfere somehow. Here is my code atm:
我在一段时间后自动打开窗户,然后在一段时间后关闭它们时遇到了一些困难。我不知道为什么,但似乎当我尝试在 window.open 和 window.close 上使用 setTimeout 时,它们会以某种方式干扰。这是我的代码自动取款机:
function topLeft() {
var myWindow = "image.png", "ONE", "width=300,height=310,top=100,left=100,menubar=no,toolbar=no,titlebar=no,statusbar=no";
setTimeout(function() {
myWindow.window.open() }, 5000);
setTimeout(function() {
myWindow.close() }, 10000);
function start() {
openClose();
}
window.onload = start;
Thanks for looking
谢谢你看
回答by jfriend00
Your code is just not right.
你的代码是不正确的。
myWindow
is a string variable.
myWindow
是一个字符串变量。
You're trying to call myWindow.window.open()
. This would generate a script error because myWindow
(a string variable) does not have a window
property.
您正在尝试调用myWindow.window.open()
。这会产生脚本错误,因为myWindow
(字符串变量)没有window
属性。
Perhaps what you mean to do is this:
也许你的意思是这样:
var myWindowURL = "image.png", myWindowName = "ONE";
var myWindowProperties = "width=300,height=310,top=100,left=100,menubar=no,toolbar=no,titlebar=no,statusbar=no";
var openWindow;
setTimeout(function() {
openWindow = window.open(myWindowURL, myWindowName, myWindowProperties);
}, 5000);
setTimeout(function() {
openWindow.close()
}, 10000);
Popup blockers in most popular browsers will only allow a new window to be opened if it is opened as a result of code running from a direct user action such as a click.
大多数流行浏览器中的弹出窗口阻止程序只允许打开一个新窗口,如果它是由于直接用户操作(例如单击)运行的代码而打开的。
Because a setTimeout()
happens some time in the future, is not considered the direct result of a user action so attempts to open windows from setTimeout()
are likely blocked by the popup blocker.
因为 asetTimeout()
发生在未来某个时间,不被视为用户操作的直接结果,因此尝试打开窗口的尝试setTimeout()
可能会被弹出窗口阻止程序阻止。
You can, of course, disable the popup blocker in your own browser, but that is only something you can do in your own browser. You can't disable popup blocking via Javascript (as that would defeat the purpose).
当然,您可以在自己的浏览器中禁用弹出窗口阻止程序,但这只能在您自己的浏览器中执行。您不能通过 Javascript 禁用弹出窗口阻止(因为这会破坏目的)。