Disable browser back Button after Logout using JavaScript in AngularJS

Last Reply 9 months ago By dharmendr

Posted 9 months ago

How to disable browser back button after logout from any of the page in angularjs or javascript

I have 5 pages;

one is login form and the other four are having logout button. When the user clicks on the logout button from any of the four pages, it will logout.

But, when the user clicks the back button in browser after, it must not go to the any of the pages where the user has clicked on the logout button.

Posted 9 months ago

Hi skp,

The following JavaScript code snippet must be placed in the HEAD section of the Page where the User must be prevented from going back.

    function preventBack() { window.history.forward(); }
    setTimeout("preventBack()", 0);
    window.onunload = function () { null };

So you need to place the code in all 4 pages having logout button.

Posted 9 months ago

Hi skp,

What you want to achieve is not possible.

You need to rethink your application logic so that it doesn't matter if the user presses the back button.

You have to handle this thing into your code.

Instead of disabling back functionality you should check user's authentication on your pages. Check whether the user has logged in.

If he doesn't that he should be redirected to the login page.