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).

The function which executes when we refresh the page is below which restricts the refresh:

window.onbeforeunload(){

}

<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.htm

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

Leave a Reply

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