javascript window.open在目标为空时不工作

javascript window.open在目标为空时不工作,javascript,Javascript,我的页面主体中有以下代码。它应该在一个新窗口中打开登录页面,然后关闭当前窗口 <script type="text/javascript"> window.open('/login', '_blank'); window.close(); </script> 如果我把blank改为self,它就行了,但这不是我想要的 有什么想法吗?window.open只会打开新窗口,不需要空白或自行打开 问题 恐怕您的浏览器不允许这样的脚本阻止弹出窗口 解决方案 您的

我的页面主体中有以下代码。它应该在一个新窗口中打开登录页面,然后关闭当前窗口

<script type="text/javascript">
    window.open('/login', '_blank');
    window.close();
</script>
如果我把blank改为self,它就行了,但这不是我想要的

有什么想法吗?

window.open只会打开新窗口,不需要空白或自行打开

问题

恐怕您的浏览器不允许这样的脚本阻止弹出窗口

解决方案

您的浏览器及其弹出窗口阻止程序通常只允许在处理用户事件(如单击)期间使用window.open。因此,您应该尝试重新设计脚本行为

也许是另一个有趣的方法

也许你应该考虑使用一个很好的弹出式的jQuery插件。这不会被你的浏览器阻止,因为它不是一个真正的弹出窗口

您可以编写window。打开'/login'而不是window。打开'/login','\u blank'。

来自文档:

window.open(URL,name,specs,replace)
关于名称参数:

Optional. Specifies the target attribute or the name of the window. The following values are supported:
_blank - URL is loaded into a new window. This is default
_parent - URL is loaded into the parent frame
_self - URL replaces the current page
_top - URL replaces any framesets that may be loaded
name - The name of the window (Note: the name does not specify the title of the new window)

因此,您不需要为空,因为默认情况下

窗口。默认情况下,打开功能用作新窗口/选项卡
Optional. Specifies the target attribute or the name of the window. The following values are supported:
_blank - URL is loaded into a new window. This is default
_parent - URL is loaded into the parent frame
_self - URL replaces the current page
_top - URL replaces any framesets that may be loaded
name - The name of the window (Note: the name does not specify the title of the new window)