Javascript 如何将 iframe 中的页面重定向到另一个页面,但我们必须留在 iframe 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28159920/
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-08-23 01:18:15  来源:igfitidea点击:

How to redirect page inside iframe to another one but we must stay in iframe

javascriptredirectiframe

提问by Lubos Baan

Could anyone help me? Is it possible to code this? Redirect page inside iframe to another one but we must stay in iframe . Without influence to parent site. I tried many scripts but nothing worked. Here is main code which i use. width and height is only for testing. I use HTML5 sandbox to prevent iframe breaks to main site. I need that to hide referer. Now parent site is showed as referer in iframed site, but i want to first site in iframe was used as referer to redirected one. Maybe it is crap, but i need that.

有人可以帮助我吗?是否可以对此进行编码?将 iframe 内的页面重定向到另一个页面,但我们必须留在 iframe 中。对父站点没有影响。我尝试了很多脚本,但没有任何效果。这是我使用的主要代码。宽度和高度仅用于测试。我使用 HTML5 沙箱来防止 iframe 中断到主站点。我需要它来隐藏引用。现在父站点在 iframe 站点中显示为引用,但我想 iframe 中的第一个站点被用作重定向站点的引用。也许这是废话,但我需要那个。

<!DOCTYPE html>
<html>
<body>


<iframe  width="1025" height="350" sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="URL"; </iframe>

</body>
</html>

回答by pyb

If you want the script to run from the frame:

如果您希望脚本从框架运行:

document.location.href = "http://...";

If you want the parent page to redirect (change) the frame location:

如果您希望父页面重定向(更改)框架位置:

HTML:

HTML:

<iframe name="myFrame" ... />

JavaScript:

JavaScript:

window.frames["myFrame"].location = "http://..."

回答by Iury Martins

$(function() {
    if ('app' !== $('body').attr('id')){
        window.setTimeout(function(){
            window.top.location.href = 'your redirect url'; 
        }, 5000);
    }
});

in IFRAME use

在 IFRAME 中使用

sandbox="allow-top-navigation allow-scripts allow-forms"