弹出窗口在ajax成功处理程序中被阻止 [英] popup window blocked in ajax success handler

查看:71
本文介绍了弹出窗口在ajax成功处理程序中被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开一个弹出窗口,以允许用户通过Twitter进行身份验证,而不必离开他们正在查看的页面.我已经在网上看到了这种技术,特别是在Disqus评论应用程序中.但是,我只能得到两个结果,要么是弹出窗口被阻止"消息,要么什么都没有发生.

I am trying to open a popup window to allow a user to authenticate with twitter without having to leave the page they are viewing. I have seen this technique used around the web, particularly with the Disqus commenting application. However, I am only able to get two results, either a 'Popup window blocked' message or nothing even happens.

我尝试使用此处

I have tried using the approaches outlined here and here. How can I fix this?

我的代码当前如下所示:

My code currently looks like:

var windowSizeArray = [ "width=200,height=200",
                            "width=300,height=400,scrollbars=yes" ];
var url = $('.twitter_popup').attr("href");
var windowName = $('.twitter_popup').attr("name");
var windowSize = windowSizeArray[$('.twitter_popup').attr("rel")];
window.open(url, windowName, windowSize);

推荐答案

当代码在脚本执行上下文中执行时,浏览器上的弹出窗口阻止程序会阻止弹出窗口.

The popup blockers on the browser block the the popups when the code is executing in the script execution context.

例如,如果我们在锚点上打开一个新窗口,则弹出窗口阻止程序将不会阻止它,但是如果我们尝试使用setTimeout打开一个新窗口,则会看到弹出窗口阻止程序将阻止它.这是因为达到超时后,上下文现在是脚本执行上下文,而不是用户操作.当我们尝试在ajax回调处理程序中打开新窗口时,这是相同的行为.

E.g If we open a new window on an anchor click the popup blocker will not block it but if we try to open a new window using setTimeout you will see that popup blocker will block this. It is because when timeout is reached the context is now script execution context instead of user action. This is the same behavior when we try to open a new window in an ajax callback handler.

我认为您也遇到类似的情况.

I think you are into similar such situation.

实时 示例

Live example

如果出于某种原因要在ajax成功处理程序本身中打开弹出窗口,则可以通过进行同步ajax调用来实现.弹出窗口不会被阻止.

If for some reason you want to open the popup in ajax success handler itself then you can achieve it by making a synchronous ajax call. The popup will not be blocked.

这篇关于弹出窗口在ajax成功处理程序中被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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