Software

Clear form on back button jquery

JQuery

Many of us, want to clear specific fields on going back page via browser back button.

Someone asked:

How do I clear the contact form data when the user clicks back on the browser to prevent spammers submitting the form multiple times by clicking back, submit over and over again?

Actually Modern browsers implement something known as back-forward cache (BFCache). When you hit back/forward button the actual page is not reloaded (and the scripts are never re-run).

So, to achieve it, you need to add code into submit click function, so, before submission just convert that field type into password. see below jquery code.

 


$("#submit").click(function () {
$('.phone').prop("type", "password");
});

 

People also ask for this solution via:

  • refresh page on back button
  • clear form on back button in html
  • browser back button keep input values
  • browser back button loses form data
  • preserve form data back button
  • javascript history back keep form data
  • back forward cache

Related Articles

Leave a Reply

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

Back to top button
Close
Close