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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745332355a4622941.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信