-3

I am trying using following code-

window.onunload = function(e){
return "Do you really want to quit without saving."
}

**

But message is also appearing if I will try to navigate from one page to another

**. I only want this functionality if user clicks on the [x] button not on any event change.

I have also tried following-

window.onbeforeunload = function(e){
 return "Somethig"
}

Note- I want to identify the event when user only closing the browser, not for any other page event.

Ashu
  • 71
  • 10
  • Possible duplicate of [how to block users from closing a window in javascript?](https://stackoverflow.com/questions/2229942/how-to-block-users-from-closing-a-window-in-javascript) – jmargolisvt Apr 28 '18 at 15:55
  • I want an explanation for detecting that event is only called for [x] buttons not from page navigation....coz page navigation also trigger these events. – Ashu Apr 28 '18 at 16:10

2 Answers2

0

Using Jquery:

$(window).unload(function() {
   //your code
});

using javasscript:

window.onbeforeunload = function(e){
    var displaymessage = 'Are you sure?';
    e = e || window.event;

    if(e)
        e.returnValue = displaymessage;

    return displaymessage;
}
Srinivas GV
  • 167
  • 7
-1

This question is a duplicate of this question.

You can't modify the default dialogue for onbeforeunload, so your best bet may be to work with it.

Also, in recent versions of Chrome, the feature has been deprecated.

Edit 09/04/2018: custom messages in onbeforeunload dialogs are deprecated since chrome-51 (cf: release note)

v3ryn3rdy
  • 21
  • 1
  • 5