I would like to use some kind of preventDefault
function to overwrite what hitting the refresh button on the browser does (or also pressing CTRL/CMD+R
).
Is there something that allows me to prevent refreshing the page?
I tried this but it doesn't seem to do anything in Firefox.
window.onunload = function(){
alert("unload event detected!");
}
I would like to use some kind of preventDefault
function to overwrite what hitting the refresh button on the browser does (or also pressing CTRL/CMD+R
).
Is there something that allows me to prevent refreshing the page?
I tried this but it doesn't seem to do anything in Firefox.
window.onunload = function(){
alert("unload event detected!");
}
Share
Improve this question
edited Jan 14, 2021 at 11:10
Erik Kaplun
38.3k15 gold badges102 silver badges113 bronze badges
asked May 29, 2013 at 5:36
Todd JenkTodd Jenk
3111 gold badge3 silver badges13 bronze badges
2
-
1
maybe
window.onunload = window.onbeforeunload = function(){
– Ohgodwhy Commented May 29, 2013 at 5:38 - this kind of works ... thanks – Todd Jenk Commented May 29, 2013 at 7:33
1 Answer
Reset to default 8You can use onbeforeunload
to prompt whether they'd like to leave:
window.onbeforeunload = function() {
return "Are you really sure?\nI don't know why anyone would want to leave my beautiful website!";
};
However, you can't override it any more than that.