0

I'm using the JS below and it's making an Ajax call when the user clicks either to Leave or Stay on the page. I only want it to fire when the user leaves.

window.onbeforeunload = function()
{    
    if(document.getElementById("SelectionsUnsaved").value=="Yes")
    {

        if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.open("GET","AjaxCallHere.asp"  ,false);
            xmlhttp.onreadystatechange=function()
            {

            }
            xmlhttp.send();

            return "****************  WARNING  ****************\n\n       YOU HAVE NOT SAVED ANY TIPS !!\n\n**********************************************";

    }
}
AussieM8
  • 37
  • 6
  • 1
    This event occurs before the user has made their choice. The `unload` event occurs after they've chosen to leave. – Related: [send an AJAX request or run a script on window closing](https://stackoverflow.com/questions/6162188/javascript-browsers-window-close-send-an-ajax-request-or-run-a-script-on-win) – Jonathan Lonowski Jun 10 '17 at 14:26
  • OK, got it .. I'll take a look at that post - thanks :) – AussieM8 Jun 10 '17 at 14:40
  • Actually, the solution on this page seems to be working for me: https://stackoverflow.com/questions/21478565/fire-javascript-function-when-user-confirms-leave-page Thanks for the info re: 'unload' event ! – AussieM8 Jun 10 '17 at 14:58

0 Answers0