window.open 没有使用 AJAX 和操纵 window.location 的弹出窗口阻止程序

2023-10-01前端开发问题
5

本文介绍了window.open 没有使用 AJAX 和操纵 window.location 的弹出窗口阻止程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

从服务器(例如 Twitter 和 Facebook)处理 OAuth 时,您很可能会将用户重定向到请求应用许可的 URL.通常,单击链接后,您通过 AJAX 将请求发送到服务器,然后返回授权 URL.

When dealing with OAuth from the server, such as Twitter and Facebook, you most likely will redirect the user to an URL asking for app permission. Usually, after clicking a link, you send the request to the server, via AJAX, and then return the authorization URL.

但是当您在收到答案时尝试使用 window.open 时,您的浏览器会阻止弹出窗口,使其无用.当然,您可以将用户重定向到新的 URL,但这会破坏用户体验,而且很烦人.您不能使用 IFRAMES,但它们是不允许的(因为您看不到地址栏).

But when you try to use window.open when the answer is received, your browser blocks the popup, making it useless. Of course, you can just redirect the user to the new URL, but that corrupts the user experience, plus it's annoying. You can't use IFRAMES, but they are not allowed (because you can't see the location bar).

那该怎么做呢?

推荐答案

答案很简单,跨浏览器运行没有任何问题.在进行 AJAX 调用时(在本例中我将使用 jQuery),只需执行以下操作.假设我们有一个带有两个按钮的表单,Login with TwitterLogin with Facebook.

The answer is quite simple, and works cross browser without any issues. When doing the AJAX call (I'll be using jQuery in this example), just do the following. Suppose we have a form with two buttons, Login with Twitter and Login with Facebook.

<button type="submit" class="login" value="facebook" name="type">Login with Facebook</button>
<button type="submit" class="login" value="twitter" name="type">Login with Twitter</button>

然后是魔法发生的 Javascript 代码

Then the Javascript code where the magic happens

$(function () {
    var
        $login = $('.login'),
        authWindow;

    $login.on('click', function (e) {
        e.preventDefault();
        /* We pre-open the popup in the submit, since it was generated from a "click" event, so no popup block happens */
        authWindow = window.open('about:blank', '', 'left=20,top=20,width=400,height=300,toolbar=0,resizable=1');
        /* do the AJAX call requesting for the authorize URL */

        $.ajax({
            url: '/echo/json/',
            type: "POST",
            data: {"json": JSON.stringify({"url": 'http://' + e.target.value + '.com'})}
            /*Since it's a jsfiddle, the echo is only for demo purposes */
        })
        .done(function (data) {
            /* This is where the magic happens, we simply redirec the popup to the new authorize URL that we received from the server */
            authWindow.location.replace(data.url);
        })
        .always(function () {
            /* You can poll if the window got closed here, and so a refresh on the main page, or another AJAX call for example */
        });
    });
});

这是 JSFiddle 中的 POC http://jsfiddle.net/CNCgG/

Here is the POC in JSFiddle http://jsfiddle.net/CNCgG/

这简单有效:)

这篇关于window.open 没有使用 AJAX 和操纵 window.location 的弹出窗口阻止程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax
Ajax call in rails 3.2.3 with will_paginate gem(使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax)...
2024-04-20 前端开发问题
6

使用视图附加或嵌入节点添加表单
Attach or embed node add form with a view(使用视图附加或嵌入节点添加表单)...
2024-04-19 前端开发问题
9

Drupal:如何使用 AJAX POST 从 REST 服务提交 webfrom
Drupal: How to submit the webfrom from REST services using AJAX POST(Drupal:如何使用 AJAX POST 从 REST 服务提交 webfrom)...
2024-04-19 前端开发问题
5