user3742691
user3742691

Reputation: 3

prevent an iframe with a page redirect in the frame from being included in browser's back button/history

I have an HTML page with an iframe, and the iframe displays a slideshow using HTML pages that load in the iframe. The user can click "Next" or "Back" within the iframe, and the iframe simply loads another HTML page. The HTML pages in the iframe are on the same domain, but in a different directory. I have blocked these clicks from the browsers back button/history by placing the following script in the body of all the pages that load in the iframe, and this part works...

    <script type="text/javascript">
    var anchors = document.getElementsByTagName('a');
    for (var i = 0; i < anchors.length; i++) {
    var anchor = anchors[i];
    anchor.addEventListener('click', function(event) {
    history.replaceState(null, null, anchor.href);
    }, false);
    }
    </script>

The problem is that there is also a link within each iframe page called "Autoplay" that loads an identical looking HTML page, except that it has the following script added to the head to make the page redirect after 5 seconds, and the browser's back button/history is capturing the redirects.

    <script type="text/JavaScript">
    setTimeout(function () {
    window.location.href = "auto2.htm";
    }, 5000);
    </script>

Does anyone know a way to block this redirect event from the browser's back button/history? Your help is appreciated! Thanks!

Upvotes: 0

Views: 2178

Answers (1)

mhu
mhu

Reputation: 18051

Change:

window.location.href = "auto2.htm";

to:

window.location.replace("auto2.htm");

Read this page for more info.

Upvotes: 1

Related Questions