检测 iFrame 中的重定向

Detect redirect in iFrame

本文关键字:重定向 iFrame 检测      更新时间:2023-09-26

我可能在这里问错了问题,所以我将提供有关我想要完成的目标的少量细节。

我使用第三方 Web 应用程序来跟踪支持票证。他们为我的用户填写的表单提供代码,并将其提交到他们的域。我想在两个不同的域上使用此表单,但不幸的是,第三方使用单个硬编码重定向值。这会导致我的一个网站在提交后切换域。

我想我可以将他们的表单嵌入到 iFrame 中,在成功提交表单时检测重定向,然后重定向到我自己的一个页面。

伪代码:

iframe.detectRedirection
if (redirect == 'xyz.html') {  
     //do my own redirection in main window.
}

所以回到我最初的问题 - 如何检测 iframe 中的重定向?

你可以

试试

$("iframe").load(function(){
    //The iframe has loaded or reloaded. 
});

检测帧加载和刷新

如果重定向发生在第二次加载时

//var to count times iframe has loaded
var timesRefreshed = 0;
//detect iframe loading/refreshing
$("iframe").load(function(){
    //if second refresh, change frame src - ie dont count first load
    if(timesRefreshed == 1){
        $(this).attr("src","my site url here");
    }
    //add to times resreshed counter
    timesRefreshed++; 
});

如果有更多阶段,只需将 X 更改为阶段数

if(timesRefreshed == x)

这不是完整的证据。 即如果其他站点添加阶段等,可能会中断,但如果您无法控制其他站点,这是我能想到的最好的。