My Code:
//hold window open on form change
window.onbeforeunload = function(e) {
if(formChanges > 0) {
if(formData != initFormData) {
if(confirm('here')) {
e.preventDefault();
}
else {
e = null;
}
}
else {
e = null;
}
}
else {
e = null;
}
};
The three vars (formChanges
, formData
, and initFormData
) are all being filled correctly, and little tests have shown that they are being read correctly within the function. The problem is that the page unloads nomatter what, and no confirmation dialog ever appears.
The console log flashes for a moment before being unloaded (I can't seem to write it's contents to file) and I can see the message Blocked confirm 'here' during beforeunload
, but it's gone before I can access it. Any help is appreciated!
My Code:
//hold window open on form change
window.onbeforeunload = function(e) {
if(formChanges > 0) {
if(formData != initFormData) {
if(confirm('here')) {
e.preventDefault();
}
else {
e = null;
}
}
else {
e = null;
}
}
else {
e = null;
}
};
The three vars (formChanges
, formData
, and initFormData
) are all being filled correctly, and little tests have shown that they are being read correctly within the function. The problem is that the page unloads nomatter what, and no confirmation dialog ever appears.
The console log flashes for a moment before being unloaded (I can't seem to write it's contents to file) and I can see the message Blocked confirm 'here' during beforeunload
, but it's gone before I can access it. Any help is appreciated!
- possible duplicate of How to show a modal dialog before beforeunload shows its own? – Denys Séguret Commented Feb 12, 2014 at 11:13
-
This is old, but a tip for people debugging. In the console (at least in Google Chrome) under settings check
Preserve log
this will keep the "flashed" errors so you can see them. – Chad Commented Dec 14, 2017 at 19:58
1 Answer
Reset to default 6WHen using onbeforeunload you have to return a string, like so:
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
source: https://developer.mozilla/en-US/docs/Web/API/Window.onbeforeunload