Javascript 单击事件后打开新窗口在 Safari、Chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4663861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:31:48  来源:igfitidea点击:

Open new window after a click event not working in Safari, Chrome

javascriptcross-browserwindow.open

提问by matthewpavkov

I'm trying to open a new window like so:

我正在尝试像这样打开一个新窗口:

$('#wrapper').click(function() {
    window.setTimeout(function() {
        //alert('hi');
        window.open("http://example.com", "ExternalLinks", "resizable=yes, scrollbars=yes, status=yes");
    }, 1000);
});

This works in Firefox, but not in Chrome or Safari (so far, I've just tested on a Mac). The alert()works in all browsers, so there seems to be something preventing the window.openfrom executing in Safari/Chrome. Furthermore, if I remove the setTimeoutand just call the window.openthen it does work in all 3 browsers. It's almost like if the window.openis nested too far away from the clickevent, then it doesn't work in Safari/Chrome.

这适用于 Firefox,但不适用于 Chrome 或 Safari(到目前为止,我刚刚在 Mac 上进行了测试)。将alert()在所有浏览器的作品,所以似乎有什么东西阻止window.open从Safari /铬中执行。此外,如果我删除setTimeout并且只调用 ,window.open那么它在所有 3 个浏览器中都有效。这几乎就像如果window.open嵌套离click事件太远,那么它在 Safari/Chrome 中不起作用。

So you know, I have an all-Flash website and I'm trying to get external links to open in a new window, so I'm reading the hash tag in the URL (ex. htp://example.com/#/facebook/) and if it matches certain items, then I'm calling window.opento open a specific URL. I don't have access to the Flash source, or I would handle this there.

所以你知道,我有一个全 Flash 网站,我试图让外部链接在新窗口中打开,所以我正在读取 URL 中的哈希标记(例如 htp://example.com/# /facebook/) 并且如果它与某些项目匹配,那么我正在调用window.open以打开特定的 URL。我无权访问 Flash 源,否则我会在那里处理。

Any ideas?

有任何想法吗?

回答by Ben Lee

Safari/Chrome have built-in pop-up blockers that stop this from working. The only javascript that is allowed to open a new window in Safari/Chrome is javascript directly attached to click handlers (and other direct user input handlers). In past versions people figured out some ways to cheat (like generating some other element -- a form or div -- and simulating user input with javascript), but newer versions are smarter about detecting this. I'd recommend re-configuring things so that you don't use a delayed pop-up -- that is the kind of thing that can generally be jarring to a user after all.

Safari/Chrome 具有内置的弹出窗口阻止程序,可以阻止它工作。唯一允许在 Safari/Chrome 中打开新窗口的 javascript 是直接附加到点击处理程序(和其他直接用户输入处理程序)的 javascript。在过去的版本中,人们想出了一些作弊的方法(比如生成一些其他元素——表单或 div——并使用 javascript 模拟用户输入),但新版本在检测这一点方面更聪明。我建议重新配置一些东西,这样你就不会使用延迟弹出窗口——毕竟这种东西通常会让用户感到不舒服。

回答by Dwight Sands

I got around this by checking the return value of window.open() for undefined. If that is true call alert() with a message for the user to to disable their popup blocker.

我通过检查 window.open() 的返回值是否为 undefined 来解决这个问题。如果这是真的,请调用 alert() 并显示一条消息,让用户禁用其弹出窗口阻止程序。

var myWin = window.open([args]);

if (myWin == undefined)
   alert('Please disable your popup blocker');

回答by Philip Enc

Another workaround
Just open a popup with ACCEPT and CANCEL options and attach the window.open
action to the ACCEPT button and it will works. It worked for me...

另一种解决方法
只需打开一个带有 ACCEPT 和 CANCEL 选项的弹出窗口,并将 window.open
操作附加到 ACCEPT 按钮,它就会起作用。它对我有用...