Good Day All

i am opening a Popup and i want to bind the "onbeforeunload" to the popup that if someone clicks on the close button in the window or close button in the page , the event should fire a javascript function that is on the page that opened the popup.

 function ProcessPayment(Downloadurl)

            var url = "";
            var popupWindow = window;

   popupWindow.onbeforeunload = function () {
                if (Downloadurl != "") {
                  .done(function () { alert('File download a success!'); })
                  .fail(function () { alert('File download failed!'); });
                else {
                    alert("No URL found");

            //open the Popup window
  , 'popUpWindow', 'height=400,width=500,left=10,top=10,resizable=no,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
             //bind the event so that when a user close the windown , we download the file 

when i try this nothing happens the event does not get work and no error in the browsers console