7

在一个文件中,我有

<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.

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

4

3 回答 3

8

此代码解决了您正在谈论的问题(特别是针对 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>
于 2015-05-13T22:06:16.680 回答
6

这似乎是一个更大的故事。请参阅此处的错误跟踪器:

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 可以处理父属性,所以也许您可以将您的应用程序从使用弹出窗口切换到使用覆盖。

于 2013-02-05T17:08:02.027 回答
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();


            }
于 2016-03-16T05:53:40.763 回答