简体   繁体   中英

Popup message when leaving page

I am looking to develop a small popup message which acts similar to the window.beforeunload function, to notify the user, that if they leave the current page, they will lose all of their data.

However the issue with the beforeunload event is it fires to often.

I would like to have the popup message fire only when a user closes the page, or clicks a link which takes them away from the current page, to ensure they are aware that their current action will result in the loss of the form data they have entered so far.

However beforeunload event goes further to fire when they refresh the page, which is not needed for this case, and also when the forum is submitted.

Could anyone advise me on the best way to develop this. I thought about using a basic confirm dialog and have it fire under the right circumstances, however is it possible to know if the user is refreshing the page, and if the forum is being submitted (without jQuery).

How can I have this dialog fire at the appropriate times?

Unfortunately, I don't think this is possible. The page unload events are very limited, for security reasons.

If you only want it to appear if the user added or changed formdata, why not check for changes in the data? If yes then return the question on beforeunload , if not do nothing.

Assuming that the form isn't too complicated, you could save form data by using Ajax call, which means there will not be a page reload. So, beforeunload will then behave as it was designed to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM