Hey,
I have been playing around with the history.length property in JavaScript and have come across a bit of a problem I don't understand.

On my localhost machine I have two pages - Page A and Page B that both have one line of script being:

document.write("Number of URLs in history list: " + history.length);

When I load new browser tab and open page A the page says I have 1 URL in history. I then open page B in the same tab and it says I now have 2 URLs in history list. Thats fine.

However, I then tried using iframes. I created an iframe with src to Page A and then changed the src of the same frame to Page B. The problem is instead of returning value of 2 (i.e. Page A and Page B) it says only 1 URL in history list but I opened two pages in the same iframe?

<body>
<div>
    <iframe src='/pageA.php' id='myframe' onload='checkHistory()' ></iframe>
</div>
</body>
</html>
<script language="javascript" type="text/javascript" >
    function checkHistory() 
    {
            document.getElementById('myframe').src='/pageB.php';
            alert("Number of URLs in history list: " + history.length);
    }
</script>

Does the history.lenght property have to be accessed differently for the iframe history something like document.getElementById('myframe').history.lenght ?

Any ideas appreciated?

try puting this in pageb.php

function history(){
parent.checkHistory();
}

<body onload="history()">

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.