javascript 检测 iFrame 中的重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10301193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:23:02  来源:igfitidea点击:

Detect redirect in iFrame

javascriptjquery

提问by mrtsherman

I may be asking the wrong question here so I will provide a small amount of detail about what I am trying to accomplish.

我可能在这里问了错误的问题,因此我将提供有关我要完成的工作的少量细节。

I use a third party web app to track support tickets. They provide the code for a form that my users fill out and it submits to their domain. I want to use this form on two different domains, but unfortunately the third party uses a single, hard coded redirect value. This causes one of my sites to switch domains after submission.

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

I was thinking I could embed their form in an iFrame, detect the redirection on successful form submission and instead redirect to one of my own pages.

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

Pseudocode:

伪代码:

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

So back to my original question - how can I detect the redirection in the iframe?

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

回答by Ed Fryed

You could try

你可以试试

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

to detect the frame loading and refreshing

检测帧加载和刷新

and if the redirect happens on the second load

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

//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++; 

});

if there are more stages just change the x to the amount of stages

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

if(timesRefreshed == x)

this isnt full proof. ie if could break if the other site adds a stage etc but its the best i can think of if you dont have any control over the other site.

这不是充分的证据。即,如果其他站点添加舞台等可能会中断,但如果您对其他站点没有任何控制权,那么它是我能想到的最好的方法。