'Make browser back button show login page without error div

I have a Login jsp that takes user email and sends it to servlet. If the email is not in database, the servlet redirects back to Login.jsp with an attribute "error". In the Login.jsp header I already have a scriplet that checks for this attribute and if it is set, it makes error msg div "display: block", else it makes "display:none". In this error msg I have a button with javascript to redirect to register page.

My Problem is: If I press browser back button when in register page, it shows login page still with the error message. I want it to instead show login page without error msg div.



Solution 1:[1]

The thing is that when you press back the browser doesn't always reload the page, that's the reason of the logging still showing the error, so what you need is to force the page to reload. Here you have a link to the solution that im giving you, hope it works How to force reloading a page when using browser back button?

Edit:

Answering to your comment, yes, it should be placed in the js file, something like this should do the trick

var perfEntries = performance.getEntriesByType("navigation");  

(function(){
    if (perfEntries[0].type === "back_forward") {
        location.reload(true);
    }
}());

This is a function that doesn't require to be called, the function calls itself when the JS file is loaded

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1