最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to overwrite the refresh button action? - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 8

You 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.

发布评论

评论列表(0)

  1. 暂无评论