0

Is it possible to prevent the browser from throwing an error message when closing a page that has data entered on a form? I have a basic javascript form that does some calculations as you enter numbers. When you try to close the tab you get:

You have entered text on “TITLE”. If you close the tab, your changes will be lost. Do you want to close the tab anyway?

I'm looking for a way to prevent this from happening since it's not needed for this form. It seems that this is the opposite behavior that most people would want.

Chris Bartow
  • 14,873
  • 11
  • 43
  • 46
  • I know of no browser that does this by default. It is probably being done by the page containing the form. – Dark Falcon Jun 01 '20 at 18:06
  • This **might** be wrapped in the `onbeforeend` function. You can try `window.onbeforeunload = function() {return "";}`. This isn't tested but I do see that returning a string, triggers that popup. – imvain2 Jun 01 '20 at 18:09
  • https://stackoverflow.com/a/19538231/3684265 – imvain2 Jun 01 '20 at 18:12
  • @imvain2 I've been playing with this idea returning true, false, null, empty string, etc. The most I can get it to do is show a standard alert box AFTER the first alert comes up. – Chris Bartow Jun 01 '20 at 18:21
  • I guess an easy solution is to not wrap the form in a form tag since you aren't technically submitting anything anyway. – imvain2 Jun 01 '20 at 18:27
  • @imvain2 Clever idea, but right now I need it for Javascript to do the calculations. May work on re-writing if all else fails. – Chris Bartow Jun 01 '20 at 18:31
  • I think there must be javascript on your page that is doing this - don't believe this is browser behaviour. Is your page created with a certain library or cms? – arbuthnott Jun 01 '20 at 19:04
  • @arbuthnott Angular. I swear I've seen it on other sites before though, but maybe you're on to something. – Chris Bartow Jun 01 '20 at 20:11

0 Answers0