【发布时间】:2012-05-24 21:30:15
【问题描述】:
我有一个包含 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 历史记录?
【问题讨论】:
-
test2.html 页面中有什么?
-
刚刚添加了test2.html,但它只是一个简单的测试页面。谢谢!
-
此INFO 可以提供更多信息。
-
这种行为在所有浏览器中都一致吗?
-
我正在 chrome、safari 和 firefox 中对此进行测试。我尝试使用 iframe 的 contentWindow 访问它,但还是一样。
标签: javascript html iframe