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
<!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.
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.
Did I do it wrong? Is there a way to track only the iframe history?
Chrome manages history on window base; not on separate frames. This behaviour is probably the cause of your problem.
Dont know if its default behaviour for all browsers btw.
Edit: 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With