【问题标题】:JavaScript history control targeted at iFrame affecting parent针对影响父级的 iFrame 的 JavaScript 历史记录控件
【发布时间】:2010-07-25 17:06:52
【问题描述】:

我刚刚注意到针对 iFrame 的 JavaScript 历史记录控件正在影响父级。代码是:

document.getElementById('iframeid').contentWindow.history.back(-1);
document.getElementById('iframeid').contentWindow.history.forward(-1);

它工作正常,直到 iframe 中没有任何东西可以后退或前进,然后它会影响父框架来回移动,这发生在所有主要浏览器、safari、opera、chrome、firefox、即 6 7 8。

有人知道如何阻止它吗?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    首先检查history对象的length

    if (document.getElementById('iframeid').contentWindow.history.length)
    {
       // your code....
    }
    

    只有当 history 对象可以导航时,该条件才会运行。

    【讨论】:

    • 这似乎不断返回值 38?
    • 检查历史长度不起作用,因为它包括来自父框架的历史长度。
    • @cappuccino: 执行alert(window.history.length) 时返回什么值?
    猜你喜欢
    • 1970-01-01
    • 2015-02-05
    • 2010-12-08
    • 2019-04-17
    • 1970-01-01
    • 2015-01-06
    • 2015-03-31
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多