4

I have requirement of show alert to the user when user try to referesh tab in browser it should get alert "if you alert this page you will loose data" but I am using jquery for this still it is not working .

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script>
$(window).on('beforeunload', function(){ 
  alert("Don't refresh you will loose your all data?");   
  //or
  confirm("are you sure want to refresh this page !");
});
</script>
Anonymous
  • 1,074
  • 3
  • 13
  • 37
  • try `window.onbeforeunload=function(e){return "";}`, notice returning something in this case is mandatory. See if this is helpful. – Abhishek Kumar Jul 26 '18 at 05:09
  • @AbhishekKumar itried that also still no response – Anonymous Jul 26 '18 at 05:11
  • you can try @Himansu Upadhyay answer – Ricardo Raz Jul 26 '18 at 05:11
  • Don't use `alert` it will just alert ,can't prevent from refresh .... Just acknowledging – Jack jdeoel Jul 26 '18 at 05:13
  • Yes, always use confirm as it provides two buttons, Ok and Cancel which returns true and false. Refer : https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm – Abhishek Kumar Jul 26 '18 at 05:14
  • Possible duplicate of [JavaScript before leaving the page](https://stackoverflow.com/questions/7080269/javascript-before-leaving-the-page) – Louys Patrice Bessette Jul 26 '18 at 05:19
  • Possible duplicate of [Execute function before refresh](https://stackoverflow.com/questions/9308336/execute-function-before-refresh) – SᴇM Jul 26 '18 at 05:21
  • Possible duplicate of [Is it possible to display a custom message in the beforeunload popup?](https://stackoverflow.com/questions/38879742/is-it-possible-to-display-a-custom-message-in-the-beforeunload-popup) – freedomn-m Jul 26 '18 at 07:34

4 Answers4

2

Try This with return

Note: NOT all browsers support this

  $(window).on("beforeunload", function (event) {
    return "Are you sure you want to leave?";
  });
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

Additional Info :

Google Chrome removed support for custom message in ver 51

Opera removed support for custom message in ver 38

Mozilla Firefox removed support for custom message in ver 44.0

Apple Safari removed support for custom message in ver 9.1

Ref : https://stackoverflow.com/a/38880926/6299088

Community
  • 1
  • 1
Dinesh Ghule
  • 3,423
  • 4
  • 19
  • 39
1

It will be like this:

 $(window).on('beforeunload', function(){ 

    return confirm("are you sure want to refresh this page !");
  });
0

Try this out:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script>
$(document).mousedown(function(e){ 
    if( e.button == 2 ) { 
        window.location.reload();
        return false; 
    } 
    return true; 
});
$(window).on('keydown', function(e){
    e = e || window.event;
    if(e.keyCode == 116 || (event.key === "r" && event.ctrlKey)){
        alert("Don't refresh you will loose your all data?");
        confirm("are you sure want to refresh this page !");
    }
});
$(window).on('beforeunload', function(){ 
  alert("Don't refresh you will loose your all data?");   
  //or
  confirm("are you sure want to refresh this page !");
});
</script>
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

Bind beforeunload event :

 $(window).bind("beforeunload",function(){ 
            return confirm("Do you want to refresh this page?");
    });
Diwa
  • 73
  • 1
  • 7