我在一段时间后打开窗口,然后在一段时间后自动关闭窗口时遇到了一些困难.我不确定为什么,但是当我尝试在window.open和window.close上使用setTimeout时,它们似乎以某种方式干扰.这是我的代码atm:
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:
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;感谢您的关注
推荐答案您的代码不正确.
myWindow是一个字符串变量.
您正尝试致电myWindow.window.open().这将产生脚本错误,因为myWindow(字符串变量)没有window属性.
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.
也许您的意思是这样:
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.
由于setTimeout()会在将来的某个时间发生,因此不被认为是用户操作的直接结果,因此,尝试从setTimeout()打开窗口的尝试可能会被弹出窗口阻止程序阻止.
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.
您当然可以在自己的浏览器中禁用弹出窗口阻止程序,但这只是您可以在自己的浏览器中执行的操作.您无法通过Java脚本禁用弹出窗口阻止功能(因为这样做会破坏目的).
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).
更多推荐
将setTimeout设置为window.open和close,在同一窗口上?
发布评论