允许window.open打开新窗口而不是弹出窗口 [英] Allow window.open to open new window and not popup

查看:131
本文介绍了允许window.open打开新窗口而不是弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个JS代码:

window.open(loginurl, '_blank');

来自某个条件,例如:

if (userloggedin) {

//popup another page

} else {

window.open(loginurl, '_blank');

}

loginurl是我想要的登录网址在新窗口中打开。

"loginurl" is an login URL that I would like to open in new window.

问题:这将在大多数浏览器(Firefox和Chrome)中被阻止,因为它的行为类似于弹出窗口。

Problem: This will be blocked in most browsers (Firefox and Chrome) because it behaves like a popup.

我想要一个仍然利用我的登录URL变量的解决方案(不改变if else语句),在新窗口中打开它,不会有任何被阻止弹出窗口的警告。

I would like a solution that will still utilizes my login URL variable (not altering the if else statement), open it in new window without any warnings of a popup that is being blocked.

我正在寻找方法,但我从来没有找到解决方案。如果有人可以提供一些提示或见解。非常感谢。

I am searching for ways but I never found a solution. If somebody can provide some tips or insights. That would be highly appreciated.

谢谢。

推荐答案

窗口由打开window.open 将始终被视为弹出窗口,当触发该功能而没有用户操作启动它时,浏览器会阻止它。

The window opened by window.open will always be regarded as a pop-up to block by browsers when the function is triggered without a user action initiating it.

这意味着,例如,这些不会被阻止:

That means that for example that these will not be blocked:

$('a').on('click.open', function(e) { e.preventDefault(); window.open('http://disney.com') });

Chrome甚至允许其他事件触发弹出窗口,而firefox不允许这样做:

Chrome even allows other events to trigger the popup, while firefox will not allow this:

$(document).on('keydown', function(e) { window.open('http://stackexchange.com') });

这将被阻止:

$(document).ready(function() { window.open('http://stackoverflow.com') });

所以,除非你触发 window.open 用户操作后,您永远无法确定您的窗口是否会被阻止。

So, unless you're triggering the window.open after a user action, you can never be sure that your window won't be blocked.

这篇关于允许window.open打开新窗口而不是弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆