【问题标题】:Redirect to Parent URL from callback after using window.open使用 window.open 后从回调重定向到父 URL
【发布时间】:2019-06-18 05:50:00
【问题描述】:

我正在尝试将回调 URL 重定向回调用 window.open 的父窗口。目前回调 URL 正在从父窗口生成的子窗口中打开。

父窗口 => 打开子窗口进行身份验证 => 用户进行身份验证并重定向到此子窗口。如何重定向到父窗口?

这是对 window.open 的调用

newwindow = window.open(response.data.auth_url,'Etsy','height=800,width=1000');
if (window.focus) {
    newwindow.focus()
}

知道如何做到这一点吗?

【问题讨论】:

    标签: javascript html angularjs


    【解决方案1】:

    要在窗口之间进行通信,请使用 Postmessage

    https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

    您的场景可能是子窗口向父窗口发送消息,父窗口又关闭子窗口并重定向到最终 url。

    【讨论】:

      【解决方案2】:

      如果其他人遇到此问题,这是我想出的解决方案。

      在我的 angularJS 控制器文件中,我从回调 URL 中检查我需要的两条信息:

      if ($state.params.oauth_token && $state.params.oauth_verifier) {
      
          /* This closes the child window */
          $window.close();
      
          var params = {
              oauth_token: $state.params.oauth_token,
              oauth_verifier: $state.params.oauth_verifier
          };
      
      
          /* process the info ... */
      
      }   
      

      我还轮询查看回调 URL 的生成时间并重定向父窗口

      win = window.open(response.data.auth_url,'Etsy','height=800,width=1000'); 
      
      var pollTimer = window.setInterval(function() { 
          try {
              if (win.document.URL.indexOf(response.data.callback_url) != -1) {
      
                  window.clearInterval(pollTimer);
      
                  $state.go("etsy.connected");
      
               }
           } catch(e) {
                 // Error Handling            
           }
      }, 500);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 2020-07-21
        • 2021-08-24
        • 2016-05-29
        • 2013-10-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多