我有一个包含 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.length
和 history.length
通过将长度增加 2 仍然给我相同的计数。
我做错了吗?有没有办法只跟踪 iframe 历史记录?
最佳答案
Chrome 在窗口基础上管理历史;不在单独的框架上。此行为可能是您遇到问题的原因。
顺便说一句,不知道它是否适用于所有浏览器的默认行为。
编辑:您必须维护您的导航历史服务器端。如另一个答案所述,在主窗口上保留数组在您还使用父窗口导航时不起作用。
关于javascript - 仅跟踪 iframe 历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10745564/