setTimeout to window.open and close, on the same window?

試著忘記壹切 提交于 2019-12-02 06:21:06

Your code is just not right.

myWindow is a string variable.

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.

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.

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).

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