Block navigation from Second Page to First Page

The script, which actually restricts the user from getting back to the first page from second page, will exists in the first page itself.

window.history.forward()

The above JavaScript function in the first page uses the history of the browser and forces it to navigate forward instead of going to the previous page. Therefore, every time the user clicks the back button or hits the backspace key, it will result in the Browser navigating or pushing the user forward and showing the same page (the page 2).

Block refresh operation

We can capture the refresh event using following handler method and then block the operation by returning any message of choice:

window.onbeforeunload(){}

Example HTML Pages to practice

page1.html

<html>
<head>
<title>
Stopping refresh and back button
</title>
<script>
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};

window.onbeforeunload = function() {
return "Do you want to leave this page?";
}
</script>
</head>
<body>
<a href="Page2.htm">Click Here...</a>
</body>
</html>

page2.html

<html>
<body>
Click the browser back button <br /> or hit the <b>Backspace</b> key.
</body>
</html>

Do let me know if it works for you.

Disable refresh and back button in browser
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *