【问题标题】:Redirect parent iFrame from nested iFrame从嵌套 iFrame 重定向父 iFrame
【发布时间】:2015-03-10 15:44:19
【问题描述】:

我在重定向 iFrames Redirect parent window from an iframe action 上看到了这个问题,但答案涉及重定向整个页面。

重定向使用的是:

window.top.location.href = "http://www.example.com"; 

有没有办法重定向嵌套的 iFrame?

即。

<browser>
    <iframe src="someSrc">
        <!--iFrame page-->
        <iframe src="nestedSrc"></iframe>
    </iframe>
</browser>

所以从nestedSrc重定向someSrc到另一个页面而不是重定向浏览器页面。

【问题讨论】:

    标签: javascript html redirect iframe


    【解决方案1】:

    如果您有权访问父 iframe 的域,您可以简单地使用以下命令重定向它:

    window.parent.location = 'http://new.location.com';
    

    小parent/top例子

    <Browser>
        <iframe1>
            <iframe2>
            </iframe2>
        </iframe1>
    </Browser>
    

    在 iframe2 中,以下情况属实:

    window.parent.parent == window.top // true
    window.parent == window.top.frames[0] // true
    

    注意:以下示例假定browser 和iframe 1 和2 都来自同一个域

    【讨论】:

    • 我知道域问题,但我知道从 iFrame 重定向浏览器允许跨域,重定向父 iFrame 是否也是如此?
    • 是的,两个iframes 之间的跨域通信也被阻止,不仅是iframe 和browser (window.top)
    猜你喜欢
    • 2011-01-06
    • 2011-02-23
    • 1970-01-01
    • 2011-06-05
    • 2012-09-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    相关资源
    最近更新 更多