【问题标题】:window.opener not set in iOS ChromeiOS Chrome 中未设置 window.opener
【发布时间】:2013-01-16 06:31:27
【问题描述】:

在一个文件中,我有

<a href="t2.html" target="_blank">go</a>

t2.html我有

<script>
document.write(window.opener);
</script>

在 iOS 上的 Safari、Mac 上的 Chrome 以及几乎所有其他浏览器上,它都会像您期望的那样打印出 [object Window]

在 iOS 上的 Chrome 上,我收到 null

如何到达打开此窗口的窗口?

【问题讨论】:

标签: ios google-chrome chrome-ios


【解决方案1】:

此代码解决了您正在谈论的问题(特别是针对不喜欢“弹出窗口”的 Chrome ios 的问题),但参考了 Paypal Adaptive Payments,它会打开“弹出窗口”并重定向到 Paypal 页面进行付款。

关键是你必须:

  1. 直接从按钮/链接单击启动 window.open
  2. 您必须使用 _blank 作为窗口“名称”(而不是自己选择)

你想要/需要的主要是:

var win;

//VERY IMPORTANT - You must use '_blank' and NOT name the window if you want it to work with chrome ios on iphone
    //See this bug report from google explaining the issue: https://code.google.com/p/chromium/issues/detail?id=136610
win = window.open(paypalURL,'_blank');

//Initiate returnFromPayPal function if the pop up window is closed
if (win && win.closed) {
    returnFromPayPal();
}

这是您可以遵循的完整代码(忽略任何不适用于您所做的事情)。

<div>
    <?php $payUrl = 'https://www.paypal.com/webapps/adaptivepayment/flow/pay?expType=mini&paykey=' . $payKey ?>

    <button onclick="loadPayPalPage('<?php echo $payUrl; ?>')" title="Pay online with PayPal">PayPal</button>
</div>
<script>
    function loadPayPalPage(paypalURL)
    {
        var ua = navigator.userAgent;
        var pollingInterval = 0;
        var win;
        // mobile device
        if (ua.match(/iPhone|iPod|Android|Blackberry.*WebKit/i)) {
            //VERY IMPORTANT - You must use '_blank' and NOT name the window if you want it to work with chrome ios on iphone
                //See this bug report from google explaining the issue: https://code.google.com/p/chromium/issues/detail?id=136610
            win = window.open(paypalURL,'_blank');

            pollingInterval = setInterval(function() {
                if (win && win.closed) {
                    clearInterval(pollingInterval);
                    returnFromPayPal();
                }
            } , 1000);
        }
        else
        {
            //Desktop device
            var width = 400,
                height = 550,
                left,
                top;

            if (window.outerWidth) {
                left = Math.round((window.outerWidth - width) / 2) + window.screenX;
                top = Math.round((window.outerHeight - height) / 2) + window.screenY;
            } else if (window.screen.width) {
                left = Math.round((window.screen.width - width) / 2);
                top = Math.round((window.screen.height - height) / 2);
            }

            //VERY IMPORTANT - You must use '_blank' and NOT name the window if you want it to work with chrome ios on iphone
                //See this bug report from google explaining the issue: https://code.google.com/p/chromium/issues/detail?id=136610
            win = window.open(paypalURL,'_blank','top=' + top + ', left=' + left + ', width=' + width + ', height=' + height + ', location=0, status=0, toolbar=0, menubar=0, resizable=0, scrollbars=1');

            pollingInterval = setInterval(function() {
                if (win && win.closed) {
                    clearInterval(pollingInterval);
                    returnFromPayPal();
                }
            } , 1000);
        }
    }

    var returnFromPayPal = function()
    {
       location.replace("www.yourdomain.com/paypalStatusCheck.php");
        // Here you would need to pass on the payKey to your server side handle (use session variable) to call the PaymentDetails API to make sure Payment has been successful
        // based on the payment status- redirect to your success or cancel/failed page
    }
</script>

【讨论】:

    【解决方案2】:

    这似乎是一个更大的故事。在此处查看 Bugtracker:

    http://code.google.com/p/chromium/issues/detail?id=136610&q=window.opener&colspec=ID%20Pri%20Mstone%20ReleaseBlock%20OS%20Area%20Feature%20Status%20Owner%20Summary

    但似乎 iframe 可以处理父属性,所以也许您可以将应用从使用弹出窗口切换到使用叠加层。

    【讨论】:

    【解决方案3】:

    如果您想将值从子级传递给父级,请使用以下代码。

    将以下代码添加到父页面:

    var hidden, state, visibilityChange;
            if (typeof document.hidden !== "undefined") {
                hidden = "hidden";
                visibilityChange = "visibilitychange";
                state = "visibilityState";
            } else if (typeof document.mozHidden !== "undefined") {
                hidden = "mozHidden";
                visibilityChange = "mozvisibilitychange";
                state = "mozVisibilityState";
            } else if (typeof document.msHidden !== "undefined") {
                hidden = "msHidden";
                visibilityChange = "msvisibilitychange";
                state = "msVisibilityState";
            } else if (typeof document.webkitHidden !== "undefined") {
                hidden = "webkitHidden";
                visibilityChange = "webkitvisibilitychange";
                state = "webkitVisibilityState";
            }
    
            // Add a listener that constantly changes the title
            document.addEventListener(visibilityChange, function () {
    
                if (localStorage.getItem("AccountName")) {
                    $("#txtGrower").val(localStorage.getItem("AccountName"));
                }
    
                if (localStorage.getItem("AccountID")) {
                    $("#hdnGrower").val(localStorage.getItem("AccountID"));
                }
    
               }, false);
    

    在子页面中添加以下内容(任何首选事件)

    function CloseChildAndLoadValuesToParent() {
    
                  localStorage.setItem("AccountName", 'MyAccountName');
                  localStorage.setItem("AccountID", 'MyAccountID');
    
                  window.close();
    
    
                }
    

    【讨论】:

      猜你喜欢
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 2011-11-03
      • 2021-11-03
      • 1970-01-01
      • 2020-02-27
      • 2011-11-12
      相关资源
      最近更新 更多