I have a page with some data and I have a function to check if it has been modified. If it has, I want to offer a confirm prompt when the page is unloaded. I am curious as to whether or not this can be done in the beforeunload function or if I am forced to enable/disable the event listener whenever the data is changed, which would be rather inefficient.
I am not sure this is what you want but from what I understand you need something like this
window.onload = function() {
window.addEventListener("beforeunload", function (e) {
const msg = 'Save Changes Before Leaving';
(e || window.event).returnValue = msg;
return msg;
});
};
if you want to check something before asking use if before the (e || window.event).returnValue = msg; return msg;
(e || window.event).returnValue = msg; return msg;
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.