javascript - Open new window after a click event not working in Safari, Chrome - Stack Overflow

I'm trying to open a new window like so:$('#wrapper').click(function() {window.setTimeou

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

$('#wrapper').click(function() {
    window.setTimeout(function() {
        //alert('hi');
        window.open("", "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.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.

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/#/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.

Any ideas?

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.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.

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.

Any ideas?

Share Improve this question asked Jan 11, 2011 at 23:29 matthewpavkovmatthewpavkov 2,9284 gold badges23 silver badges37 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 15

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.

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.

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

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

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...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1738388437a4046609.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信