0

I was reading jQuery documentation for the function .unload() given in the jQuery API Documentation

It is written clearly that .unload() will is deprecated in versions after 1.8 and removed completely in 3.x.

I have a local intranet application that is dependent upon this .unload() function.

Do I have to manually rely on window.onbeforeunload function and see that a particular browser supports this or not, or can anyone help me in finding a more generic solution for the same.

As suggested by Kevin, I tried

$(window).on("unload", function(e){
    return confirm("Do you really want to exit");
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
Testing JQuery Unload in 3.x Version

Fiddle

But it is not working

Makyen
  • 31,849
  • 12
  • 86
  • 121
vibs2006
  • 6,028
  • 3
  • 40
  • 40
  • 3
    replace `.unload()` with `.on('unload', function())` which is not deprecated. – Kevin Kloet Sep 27 '17 at 08:34
  • 2
    _It is written clearly that unload function will not be depreciated in future versions after 1.8_ because it is already deprecated in 1.8 there are nothing left to deprecate after 1.8. Use the `on` method , which is the jQuery alternative of `adddEventListener` as suggested by Kevin – Sagar V Sep 27 '17 at 08:35
  • @KevinKloet as per you I have used **.on** method now. Can u help me in finding out that why my fiddle is not working https://jsfiddle.net/vibs2006/bqge8x22/ – vibs2006 Sep 27 '17 at 08:53
  • @SagarV my same comment (previous to this comment) question to you too. Pl guide. – vibs2006 Sep 27 '17 at 08:54

1 Answers1

5

unload is a shorthand method in jQuery and is deprecated. When using on, you have to use the exact same event as present in JavaScript removing on.

Here, use beforeunload instead of unload

//$(window).on("unload", function(e){
//              ^^^
$(window).on("beforeunload", function(e) {
//            ^^^
  return confirm("Do you really want to exit");
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
Testing JQuery Unload in 3.x Version
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • @vibs2006, It does work in Firefox. The user just has to have interacted with the page appropriately in order for Firefox to permit you to have the user confirm leaving. You should [read the documentation](https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload). Firefox is blocking your use of `confirm`, under some conditions, but the `onbeforeunload` event fires. Note that Firefox also does not permit a custom message in the confirmation to leave the page. You need to demonstrate a case where `$(window).unload()` works, but `$(window).on('beforeunload')` doesn't. – Makyen Sep 28 '17 at 23:46
  • 1
    @vibs2006 Try the difference on Firefox in [this JSFiddle](https://jsfiddle.net/wm1tu9qv/) between having not typed anything into the text area and after you type something into the text area. – Makyen Sep 28 '17 at 23:59
  • Thanks @Sagar. Firefox now seems to be working fine. – vibs2006 Sep 29 '17 at 07:58
  • Not working in Android phone Chrome browser – Meenakshi Khandelwal May 18 '21 at 19:04
  • @MeenakshiKhandelwal https://stackoverflow.com/q/35779372/2427065 – Sagar V May 18 '21 at 19:12
  • @SagarV I used `window.onunload = window.onbeforeunload` but did not work in Android Chrome.. other browsers are fine even in IOS Chrome is also fine. – Meenakshi Khandelwal May 19 '21 at 20:13