How to prompt when user typed something in the textbox and unknowingly close the browser?
Also if the user doesn’t type anything in the input field and if close the browser, the browser should close without prompting. Browser should prompt only if the user changed something in the field. If I use onbeforeunload event, it promts for everything even when saving the page. Is there any way to solve my problem in javascript. Please help by giving me apt codes.
Check this link: https://web.archive.org/web/20211020134123/https://www.4guysfromrolla.com/webtech/100604-1.shtml
It gives you information on how to handle onbeforeunload event.
The idea is to have a global flag on the page. When any change is done to the fields, this flag is set to true. When clicked on save button, then this flag needs to be set to false.
In the onbeforeunload event, check whether the flag is true, then show the message accordingly.
Another way of doing the things, would be to set the needToConfirm flag when any change is made to the fields via onchange events.