I have seen the same function is multiple sites including SO. I am also trying to achieve this. Here is the code so far.
<script>
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';
myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
var confirmationMessage = ' ';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
</script>
The problem is above code is working fine in chrome and IE but when I tried to test in on Firefox it is not working. I checked firebug but there is no error. I updated the firefox and version are 47.0.1.
But issue not fixed.
Any advice would be helpful.
Guys if you have code better than this then it would be also helpful.
I have seen the same function is multiple sites including SO. I am also trying to achieve this. Here is the code so far.
<script>
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';
myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
var confirmationMessage = ' ';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
</script>
The problem is above code is working fine in chrome and IE but when I tried to test in on Firefox it is not working. I checked firebug but there is no error. I updated the firefox and version are 47.0.1.
But issue not fixed.
Any advice would be helpful.
Guys if you have code better than this then it would be also helpful.
Share Improve this question edited Jul 11, 2016 at 12:22 Florian Schaal 2,6604 gold badges41 silver badges59 bronze badges asked Jul 11, 2016 at 12:05 RoxxRoxx 3,98622 gold badges100 silver badges165 bronze badges 1- use onbeforeunload() – JYoThI Commented Jul 11, 2016 at 12:07
5 Answers
Reset to default 5Use below code, I tested it in firefox and is working fine :
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome
});
Well I tested this code and it is working on firefox and chrome even with firebug console closed :
<script>
window.onbeforeunload = function (e) {
var message = "Are you sure ?";
var firefox = /Firefox[\/\s](\d+)/.test(navigator.userAgent);
if (firefox) {
//Add custom dialog
//Firefox does not accept window.showModalDialog(), window.alert(), window.confirm(), and window.prompt() furthermore
var dialog = document.createElement("div");
document.body.appendChild(dialog);
dialog.id = "dialog";
dialog.style.visibility = "hidden";
dialog.innerHTML = message;
var left = document.body.clientWidth / 2 - dialog.clientWidth / 2;
dialog.style.left = left + "px";
dialog.style.visibility = "visible";
var shadow = document.createElement("div");
document.body.appendChild(shadow);
shadow.id = "shadow";
//tip with setTimeout
setTimeout(function () {
document.body.removeChild(document.getElementById("dialog"));
document.body.removeChild(document.getElementById("shadow"));
}, 0);
}
return message;
};
</script>
I found this script crossbrowser-onbeforeunload.js. It is cross-browser patible.
It's work for me by this code.
window.onbeforeunload = function () {
return 'Are you sure? Your work will be lost. ';
};
window.onbeforeunload is a good way to go. But it seems to be working in Firefox only if user has interaction with the website (click or scroll). Otherwise function doesn't trigger. Chrome, on the other hand works great.
Note: To bat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with; some don't display them at all
Documentation
Try this
window.addEventListener('beforeunload', function (event) {
event.preventDefault();
event.returnValue = 'Are you sure you want to leave this page?';
});