• <tfoot id='sHNTO'></tfoot>
      <bdo id='sHNTO'></bdo><ul id='sHNTO'></ul>
    <legend id='sHNTO'><style id='sHNTO'><dir id='sHNTO'><q id='sHNTO'></q></dir></style></legend>

      <small id='sHNTO'></small><noframes id='sHNTO'>

      <i id='sHNTO'><tr id='sHNTO'><dt id='sHNTO'><q id='sHNTO'><span id='sHNTO'><b id='sHNTO'><form id='sHNTO'><ins id='sHNTO'></ins><ul id='sHNTO'></ul><sub id='sHNTO'></sub></form><legend id='sHNTO'></legend><bdo id='sHNTO'><pre id='sHNTO'><center id='sHNTO'></center></pre></bdo></b><th id='sHNTO'></th></span></q></dt></tr></i><div id='sHNTO'><tfoot id='sHNTO'></tfoot><dl id='sHNTO'><fieldset id='sHNTO'></fieldset></dl></div>
      1. window.open 没有使用 AJAX 和操纵 window.location 的弹出窗口阻止程序

        window.open without popup blocker using AJAX and manipulating the window.location(window.open 没有使用 AJAX 和操纵 window.location 的弹出窗口阻止程序)
        <legend id='lJocD'><style id='lJocD'><dir id='lJocD'><q id='lJocD'></q></dir></style></legend>
              <tbody id='lJocD'></tbody>

            <i id='lJocD'><tr id='lJocD'><dt id='lJocD'><q id='lJocD'><span id='lJocD'><b id='lJocD'><form id='lJocD'><ins id='lJocD'></ins><ul id='lJocD'></ul><sub id='lJocD'></sub></form><legend id='lJocD'></legend><bdo id='lJocD'><pre id='lJocD'><center id='lJocD'></center></pre></bdo></b><th id='lJocD'></th></span></q></dt></tr></i><div id='lJocD'><tfoot id='lJocD'></tfoot><dl id='lJocD'><fieldset id='lJocD'></fieldset></dl></div>

              <bdo id='lJocD'></bdo><ul id='lJocD'></ul>

                <tfoot id='lJocD'></tfoot>
                1. <small id='lJocD'></small><noframes id='lJocD'>

                  本文介绍了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 的弹出窗口阻止程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Difference between fetch, ajax, and xhr(fetch、ajax 和 xhr 之间的区别)
                  How to use JSONP on fetch/axios cross-site requests(如何在 fetch/axios 跨站请求中使用 JSONP)
                  Django returns 403 error on POST request with Fetch(Django 使用 Fetch 对 POST 请求返回 403 错误)
                  Open page in new window without popup blocking(在新窗口中打开页面而不阻止弹出窗口)
                  Javascript Confirm popup Yes, No button instead of OK and Cancel(Javascript 确认弹出窗口是,否按钮而不是确定和取消)
                  execCommand(#39;copy#39;) does not work in Ajax / XHR callback?(execCommand(copy) 在 Ajax/XHR 回调中不起作用?)
                    <bdo id='iakeF'></bdo><ul id='iakeF'></ul>

                        <legend id='iakeF'><style id='iakeF'><dir id='iakeF'><q id='iakeF'></q></dir></style></legend>

                          <small id='iakeF'></small><noframes id='iakeF'>

                            <tbody id='iakeF'></tbody>
                          <tfoot id='iakeF'></tfoot>
                        1. <i id='iakeF'><tr id='iakeF'><dt id='iakeF'><q id='iakeF'><span id='iakeF'><b id='iakeF'><form id='iakeF'><ins id='iakeF'></ins><ul id='iakeF'></ul><sub id='iakeF'></sub></form><legend id='iakeF'></legend><bdo id='iakeF'><pre id='iakeF'><center id='iakeF'></center></pre></bdo></b><th id='iakeF'></th></span></q></dt></tr></i><div id='iakeF'><tfoot id='iakeF'></tfoot><dl id='iakeF'><fieldset id='iakeF'></fieldset></dl></div>