Javascript – How to get “Confirm form resubmission” when user trying to refresh browser

javascript, jquery, spring-mvc

I have a page that doesn't allow user to refresh the page by clicking browser refresh button or pressing F5. So I want to simulate 'form resubmission' in order to show prompt message.

can someone guide me an approach to implement it?

Is there any cross-browser solution available for that?

Best Solution

To refresh without getting the prompt the page must have been fetched using HTTP GET

Make the form have method="GET" instead of method="POST" (and fix server processes to work with this change as apropriate)

Alternatively cache the post data in a session and redirect to the display page using HTTP code 303 immediately after form submission.

If you want to cause the prompt, make the link that takes the user to the page into a submit button on a POST form. if a user arrives with a GET request have serve them a page having javascript that submits a form converting the request into a POST request.