Track only iframe history

Posted by evanwong on Stack Overflow See other posts from Stack Overflow or by evanwong
Published on 2012-05-24T21:30:15Z Indexed on 2012/06/05 16:40 UTC
Read the original article Hit count: 184

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html