'window.open'已被Chrome更改事件阻止 [英] 'window.open' blocked by Chrome with change event

查看:262
本文介绍了'window.open'已被Chrome更改事件阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开一个基于select元素上的onChange事件的窗口而不会被Chrome的弹出窗口阻止程序阻止。

I am trying to open a window based on an onChange event on a select element without getting blocked by Chrome's popup blocker.

此处演示了此问题。

https://jsfiddle.net/yyfe0824/1/

<select class="dropdown-select" onChange="window.open('https://www.google.com');">
    <option value="uno">Uno</option>
    <option value="dos">Dos</option>
    <option value="tres">Tres</option>
</select>

<input type="button" onClick="window.open('https://www.google.com');" value="click me">

在点击我按钮上调用window.open没有问题,但是如果你尝试更改选择下拉列表,chrome将阻止弹出窗口。

There is no problem with a window.open call on the 'click me' button, but if you try to change the select dropdown, chrome will block the popup.

到目前为止,此问题的答案特定于onClick事件。进行研究表明,如果Chrome检测到它不是通过某种处理程序触发的用户,则会阻止弹出窗口,因此我特意尝试内联调用函数,而不是使用其他命名函数。

So far, answers to this problem have been specific to the onClick event. Doing research reveals that Chrome will block popups if it detects that it is not user triggered via some sort of handler, so I'm specifically trying to call the function inline, rather than using another named function.

这是window.open的预期行为,特别是onChange,如果有的话,是否有任何特定的解决方法? (除了首先将结构更改为点击事件。)

Is this the intended behavior of window.open, specifically for onChange and if so, is there any particular workaround? (Aside from changing the structure to be a click event in the first place.)

推荐答案

这是设计上的唯一一次浏览器不阻止 window.open 当你处理点击事件时。

That is by design, the only time browsers don't block window.open is when you are handling a click event.

我的建议是提供一个链接,当用户从下拉列表中选择时,该链接会发生变化。

My suggestion is to provide a link that changes when users select from the dropdown.

我建议不要打开弹出窗口,因为用户没有当你从下拉列表中选择时,期望弹出窗口,这就是弹出窗口阻止程序通常不允许这样做的原因。即使您发现可在浏览器中运行的内容( https://jsfiddle.net/yyfe0824/5/在Firefox中,它可能会在未来中断。

I advise against opening a popup because users don't expect a popup when you select from a drop down, that is why popup blockers don't typically allow this. Even if you find something that works in a browser (https://jsfiddle.net/yyfe0824/5/ in Firefox), it could break in the future.

这篇关于'window.open'已被Chrome更改事件阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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