javascript - 仅跟踪 iframe 历史记录

标签 javascript html iframe

我有一个包含 iframe 的页面,我只想跟踪 iframe 的历史记录。 我试着像这样使用历史对象:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script type="text/javascript">
        function checkFrameHistory() {
            alert(window.frames["test2"].history.length);
        }

        function checkThisHistory() {
            alert(history.length);
        }
        </script>
    </head>
    <body>

        <iframe name="test2" src="test2.html"></iframe>

        <div>
            <input type="button" onclick="checkFrameHistory()" value="Frame history"/>
            <input type="button" onclick="checkThisHistory()" value="This history"/>
        </div>

    </body>
</html>

test2.html

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script type="text/javascript">
        function checkMyHistory() {
            alert(history.length);
        }
        </script>
    </head>
    <body>
    <div>
        Test2
    </div>

    <div>
        <input type="button" onclick="checkMyHistory()" value="My history"/>
    </div>

    </body>
</html>

但它实际上给了我包括父窗口在内的历史记录。

例如,我在主窗口中转到另一个站点,然后返回到我的 iframe 测试站点。 window.frames["test2"].history.lengthhistory.length 通过将长度增加 2 仍然给我相同的计数。

我做错了吗?有没有办法只跟踪 iframe 历史记录?

最佳答案

Chrome 在窗口基础上管理历史;不在单独的框架上。此行为可能是您遇到问题的原因。

顺便说一句,不知道它是否适用于所有浏览器的默认行为。

编辑:您必须维护您的导航历史服务器端。如另一个答案所述,在主窗口上保留数组在您还使用父窗口导航时不起作用。

关于javascript - 仅跟踪 iframe 历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10745564/

相关文章:

javascript - 如何在使用 ngBindHtml 绑定(bind) View 时缩短 html 字符串的内容文本

html - 在网站中嵌入 JS 控制台

javascript - 录制音频时显示进度条

javascript - Knockout.js 使用拦截器扩展值绑定(bind)

html - 如何垂直对齐底部水平滚动的两个表

jquery - contentWindow.document.execCommand ('print' , false, null) 在 Firefox 中不起作用

jquery - 在一个 div 中实现 jQuery,该 div 的内容刚刚用 jQuery 的 .load() 加载

android - 将 iframe 放在 android 应用程序上

javascript - 无法使用 'in' 运算符在 null 中搜索 'error'

javascript - 如何在javascript中下载具有自定义名称的mp3文件