在Safari,Chrome中无法使用点击事件后打开新窗口 [英] Open new window after a click event not working in Safari, Chrome

查看:177
本文介绍了在Safari,Chrome中无法使用点击事件后打开新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开一个新窗口,如下所示:

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);
});

这适用于Firefox,但不适用于Chrome或Safari Mac)。 alert()在所有浏览器中都有效,因此似乎有些东西阻止了 window.open /铬。此外,如果我删除 setTimeout ,只需调用 window.open ,那么它可以在所有3个浏览器中工作。这几乎就像 window.open 嵌套在距离点击事件太远,那么它不工作在Safari / Chrome中。

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.open from executing in Safari/Chrome. Furthermore, if I remove the setTimeout and just call the window.open then it does work in all 3 browsers. It's almost like if the window.open is nested too far away from the click event, then it doesn't work in Safari/Chrome.

所以你知道,我有一个全闪存网站,我想尝试在新窗口中打开外部链接, m读取URL中的哈希标记(例如htp://example.com/#/facebook/),如果它匹配某些项目,则我调用 window.open 打开特定的URL。

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.open to open a specific URL. I don't have access to the Flash source, or I would handle this there.

有没有想法?

推荐答案

Safari / Chrome有内置的弹出窗口阻止程序阻止这种工作。允许在Safari / Chrome中打开一个新窗口的唯一的javascript是javascript直接附加到单击处理程序(和其他直接用户输入处理程序)。在过去的版本中,人们想出了一些欺骗的方法(例如生成一些其他元素 - 一个表单或div,并使用javascript模拟用户输入),但是更新的版本更聪明地检测这一点。我建议重新配置的东西,使你不使用延迟的弹出窗口 - 这是一种通常可以刺激给用户的东西。

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中无法使用点击事件后打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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