将setTimeout设置为window.open和close,在同一窗口上?

编程入门 行业动态 更新时间:2024-10-28 10:23:14
本文介绍了将setTimeout设置为window.open和close,在同一窗口上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一段时间后打开窗口,然后在一段时间后自动关闭窗口时遇到了一些困难.我不确定为什么,但是当我尝试在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,在同一窗口上?

本文发布于:2023-08-06 02:11:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   窗口   在同一   setTimeout   window

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!