window.opener在重定向后的javascript popup中未定义 [英] window.opener undefined in javascript popup after redirect

查看:123
本文介绍了window.opener在重定向后的javascript popup中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用window.open()显示付款对话框的网站存在问题。



弹出窗口中的页面重定向到不同的域,重定向到到结果页面。在结果页面上,我尝试在 window.opener 上设置一个属性,表示付款正常。



这适用于某些用户。但是,其他用户会得到一个错误,指出 window.opener 未定义。

使用这些简单网页可以重新创建问题:

index.html
(打开弹出窗口)

 <!DOCTYPE html> 
< html lang =en>
< body>
< input type =buttonvalue =Popuponclick =openPaymentWindow(); />

< div id =resultstyle =background-color:silver;/>

< script type =text / javascript>
函数openPaymentWindow(){
win = window.open('popup.html','popup');
}
< / script>
< / body>
< / html>

popup.html
(重定向到不同的域)


 <!DOCTYPE html> 
< html>
< body onload =document.forms [0] .submit();>
< form action =http:// [other_domain] /payment.html>
< / form>
< / body>
< / html>

payment.html
(重定向回原始网域)

 <!DOCTYPE html> 
< html>
< body onload =document.forms [0] .submit();>
< form action =http:// [original_domain] /result.html>
< / form>
< / body>
< / html>

result.html
(设置索引属性页面)

 <!DOCTYPE html> 
< html>
< body>
< input type =buttonvalue =Call toponclick =callTop(); />

< script type =text / javascript>
函数callTop(){
//这里,window.opener(和top.opener)对于某些用户是未定义的
window.opener.document.getElementById('result')。style。背景='绿色';
}
< / script>
< / body>
< / html>

由于并非所有用户都受到影响,我的猜测是它与一些安全设置有关。但我根本找不到在哪里找到它们 - 或者如何在我自己的电脑上复制错误。 有一个实现你想要的方式,虽然它可能不是很干净。
所以 index.html result.html 位于<$ c $相同的域和脚本中c> index.html 包含对弹出窗口对象的引用。这意味着如果您在 result.html 中的 window 对象上设置了一些属性,您将能够读取该属性 win 变量位于 index.html 中。唯一的问题是你不知道什么时候该属性被设置,所以你将不得不使用setInterval来检查它。



Index.html

 函数openPaymentWindow(){
var win = window.open('popup。 html','popup');

var successPoller = setInterval(function(){
try {
if(typeof win.PAYMENT_SUCESS!=='undefined'&&win; PAYMENT_SUCESS){
document.getElementById('result')。style.background ='green';
clearInterval(successPoller);
}
}
catch(error){
//处理错误
}
},500);
}

result.html
p>

 函数callTop(){
window.PAYMENT_SUCCESS = true;
}


I have a problem with a website that displays a payment dialog using window.open().

The page in the popup redirects to a different domain that redirects back to a result page. On the result page, I try to set a property on window.opener to signal that the payment is ok.

This works for some users. Other users, however, get an error saying that window.opener is undefined.

The problem can the recreated using these simple pages:

index.html (Opens the popup)

<!DOCTYPE html>
<html lang="en">
<body>
    <input type="button" value="Popup" onclick="openPaymentWindow();" />

    <div id="result" style="background-color: silver;"/>

    <script type="text/javascript">
        function openPaymentWindow() {
            win = window.open('popup.html', 'popup');            
        }
    </script>    
</body>
</html>

popup.html (Redirects to a different domain)

<!DOCTYPE html>
<html>
<body onload="document.forms[0].submit();">
    <form action="http://[other_domain]/payment.html">
    </form>
</body>
</html>

payment.html (Redirects back to the original domain)

<!DOCTYPE html>
<html>
<body onload="document.forms[0].submit();">
    <form action="http://[original_domain]/result.html">
    </form>    
</body>
</html>

result.html (Sets a property on the index page)

<!DOCTYPE html>
<html>
<body>
    <input type="button" value="Call top" onclick="callTop();" />

    <script type="text/javascript">
        function callTop() {
            // Here, window.opener (and top.opener) is undefined for some users
            window.opener.document.getElementById('result').style.background = 'green';
        }
    </script>  
</body>
</html>

Since not all users are affected, my guess is that it has to do with some security settings. But I simply cannot figure out where to find them - or how to replicate the error on my own pc.

解决方案

There is a way to achieve what you want, it may not be very clean though. So index.html and result.html are on the same domain and script in index.html contains a reference to window object of a popup. This means that if you set some property on window object in result.html you will be able to read that property on win variable in index.html. The only problem is you don't know when exactly the property is being set, so you will have to use setInterval to check it.

Index.html

function openPaymentWindow() {
    var win = window.open('popup.html', 'popup');

    var successPoller = setInterval(function() {
        try {
            if(typeof win.PAYMENT_SUCESS !== 'undefined' && win.PAYMENT_SUCESS) {
                document.getElementById('result').style.background = 'green';
                clearInterval(successPoller);
            }
        }
        catch (error) {
            // handle error
        }
    }, 500);
}

result.html

 function callTop() {
      window.PAYMENT_SUCCESS = true;  
 }

这篇关于window.opener在重定向后的javascript popup中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆