admin管理员组

文章数量:1399022

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 pletely 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=".2.1.min.js"></script>
Testing JQuery Unload in 3.x Version

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 pletely 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./jquery-3.2.1.min.js"></script>
Testing JQuery Unload in 3.x Version

Fiddle

But it is not working

Share Improve this question edited Sep 28, 2017 at 23:54 Makyen 33.4k12 gold badges92 silver badges125 bronze badges asked Sep 27, 2017 at 8:31 vibs2006vibs2006 6,5584 gold badges41 silver badges43 bronze badges 4
  • 3 replace .unload() with .on('unload', function()) which is not deprecated. – Kevin Kloet Commented Sep 27, 2017 at 8: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 Commented Sep 27, 2017 at 8: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 jsfiddle/vibs2006/bqge8x22 – vibs2006 Commented Sep 27, 2017 at 8:53
  • @SagarV my same ment (previous to this ment) question to you too. Pl guide. – vibs2006 Commented Sep 27, 2017 at 8:54
Add a ment  | 

1 Answer 1

Reset to default 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./jquery-3.2.1.min.js"></script>
Testing JQuery Unload in 3.x Version

本文标签: javascriptWhat is a foolproof alternative to (window)unload()Stack Overflow