仅追踪iframe历史记录 [英] Track only iframe history
问题描述
我有一个包含iframe的页面,我只想跟踪iframe的历史记录。
我尝试使用这样的历史对象:
I have a page which contains an iframe and I want to track the history of the iframe only. I tried to use the history object like this:
<!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
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>
但它实际上给了我包括父窗口的历史记录。
but it actually gave me the history including the parent window.
例如,我去了主窗口中的另一个站点,然后回到我的iframe测试站点。 window.frames [test2]。history.length
和 history.length
仍然给了我相同的计数增加长度2.
For instance, I went to another site in the main window and came back to my iframe test site. Both window.frames["test2"].history.length
and history.length
still gave me the same count by increasing the length by 2.
我做错了吗?有没有办法只跟踪iframe的历史?
Did I do it wrong? Is there a way to track only the iframe history?
推荐答案
Chrome在窗口基础上管理历史记录;不在单独的框架上。此行为可能是您的问题的原因。
Chrome manages history on window base; not on separate frames. This behaviour is probably the cause of your problem.
不知道它的所有浏览器的默认行为btw。
Dont know if its default behaviour for all browsers btw.
编辑:你必须维护你的navigationhistory服务器端。正如另一个答案中所述,在主窗口中保留一个数组,当您还使用父窗口进行导航时,它不起作用。
You have to maintain your navigationhistory server side. Keeping an array on the mainwindow, as stated in another answer, doesnt work when you also navigate with the parent window.
这篇关于仅追踪iframe历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!