window.opener未在iOS Chrome中设置

时间:2013-02-01 00:53:19

标签: ios google-chrome chrome-ios

在一个文件中,我有

<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

如何进入打开此窗口的窗口?

3 个答案:

答案 0 :(得分:7)

此代码解决了您所讨论的问题(特别是针对Chrome ios不喜欢的问题&#34;弹出窗口&#34;),但是参考Paypal自适应付款,它打开了一个&#34;弹出&#34; 34;并重定向到Paypal页面进行付款。

关键是你必须:

  1. 直接从按钮/链接点击
  2. 启动window.open
  3. 您必须使用_blank作为窗口&#34; name&#34; (而不是选择你自己的)
  4. 您想要/需要的主要是:

    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>
    

答案 1 :(得分:5)

这似乎是一个更大的故事。请在此处查看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可以处理父属性,所以也许你可以将你的应用从使用弹出窗口转换为使用叠加层。

答案 2 :(得分:0)

如果要将值从子节点传递给父节点,请使用以下代码。

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

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();


            }