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

javascript - Confirmation on close or refresh page not working at all - Stack Overflow

programmeradmin1浏览0评论

When the user closes the tab or refreshes the page, the site must display a popup to confirm this. I tried this code:

window.onbeforeunload = function (e) {
    return confirm("Are you sure you want to leave this page?");
};

This didn't work in either firefox or chrome. In firefox no popup came up. And in chrome the default one didn't get overridden either. I even tried using the following code but to no avail:

window.onbeforeunload = function (e) {
    var dialogText = 'Are you sure about this?';
    e.returnValue = dialogText;
    return dialogText;
};

How do I solve this issue? Any code snippets would be helpful. Thank you.

When the user closes the tab or refreshes the page, the site must display a popup to confirm this. I tried this code:

window.onbeforeunload = function (e) {
    return confirm("Are you sure you want to leave this page?");
};

This didn't work in either firefox or chrome. In firefox no popup came up. And in chrome the default one didn't get overridden either. I even tried using the following code but to no avail:

window.onbeforeunload = function (e) {
    var dialogText = 'Are you sure about this?';
    e.returnValue = dialogText;
    return dialogText;
};

How do I solve this issue? Any code snippets would be helpful. Thank you.

Share Improve this question asked Aug 27, 2016 at 18:50 Kartik ShenoyKartik Shenoy 3283 silver badges14 bronze badges 5
  • Possible duplicate of How to create popup window when browser close – Matthijs Commented Aug 27, 2016 at 18:54
  • Possible duplicate of Trying to detect browser close event – Bobulous Commented Aug 27, 2016 at 18:54
  • This is already answered in this similar post stackoverflow./questions/10310177/… – Rodney Zanoria Commented Aug 27, 2016 at 18:55
  • I've already explained it here: stackoverflow./questions/39128680/… – user6586783 Commented Aug 27, 2016 at 18:57
  • Possible duplicate of Is it possible to display a custom message in the beforeunload popup? – Dekel Commented Aug 27, 2016 at 21:49
Add a ment  | 

3 Answers 3

Reset to default 3

I found this snippet on the internet:

window.onbeforeunload = function (e) {
        return "Please click 'Stay on this Page' if you did this unintentionally";
    };

This is working perfectly as required. I found out that you actually don't need to add any confirm call. If you just return a string, this will prompt the browser to confirm leaving the page. Most mercial browsers have this functionality supported by default.

From Firefox's documentation:

To bat unwanted pop-ups, browsers may not display prompts created in beforeunload event handlers unless the page has been interacted with.

You cannot show pop-up if user didn't interacted with the page before.

Starting with Firefox 4, Chrome 51, Opera 38 and Safari 9.1, a generic string not under the control of the webpage will be shown instead of the returned string. For example, Firefox displays the string "This page is asking you to confirm that you want to leave - data you have entered may not be saved."

-- from Mozilla Developer Docs

发布评论

评论列表(0)

  1. 暂无评论