Disable browser back button using JavaScript

Suppose in our application we have two web-pages namely first.aspx and second.aspx and let us imagine a situation that someone is redirecting to second.aspx from first.aspx and our requirement is that if we click on the backward button of the browser then this will not allow us to get back to first.aspx page.

To achieve this we need to put the following piece of code in the head section of first.aspx.

<script type = "text/javascript" >
    function preventBack() { window.history.forward(); }
    setTimeout("preventBack()", 0);
    window.onunload = function () { null };
</script>

Now, run your application and check it out..

Ebook Download
View all
Learn
View all