0

I need to check if user closed browser. There is no reliable way, but the most accurate way seems to use onbeforeunload and check if a link or a button was clicked. I will also add f5 and some other extra checking.

But I have problem with button. If I click button, ajax call will be made even if there is if(window.link_was_clicked==false) condition.

<button onclick="setLocation('http://dev.site.com/checkout/')" class="button" title="Checkout" type="button"><span><span>Checkout</span></span></button>

And script:

jQuery(document).on('click', 'button', function(event) {
  window.link_was_clicked= true;
});  

window.onbeforeunload = function() {
  if(window.link_was_clicked==false){                
    //make ajax call      
  }
};

It seems the problem is because there is setLocation function attached to button onclick. Is there any way to trigger jQuery(document).on first?

8
  • which button are you talking about? are you talking about the browser close button? Commented Apr 10, 2014 at 12:26
  • stackoverflow.com/questions/15277403/… Commented Apr 10, 2014 at 12:28
  • Where is your HTML? If you aren't using a <button> tag then your jQuery won't select anything. Commented Apr 10, 2014 at 12:28
  • Your HTML is too sparse. Commented Apr 10, 2014 at 12:30
  • @Sushil I am talking about <button> html tag. I put now this into question. Commented Apr 10, 2014 at 12:35

2 Answers 2

0

That variable doesnt exist on before load. so add it on the top and try again

     window.link_was_clicked=window.link_was_clicked||false;
     jQuery(document).on('click', 'button', function(event) {
         window.link_was_clicked= true;
      });  

     window.onbeforeunload = function() {
         if(window.link_was_clicked==false){                
        //make ajax call      
     }
    };
Sign up to request clarification or add additional context in comments.

1 Comment

In real code it is window.link_was_clicked= false;, I just wanted to keep example short and easy. So this is not the problem.
0

Since you haven't provided the code for setLocation(), I'll assume that that's where the problem is. Now, regarding a change to the order of execution of your click event handlers:

It is in general bad to have both embedded onclick and jquery-bound handlers, it leads to all sorts of confusion. If you can't change the html, however, you could do what this guy suggests: https://stackoverflow.com/a/7507888/2685386

$(document).ready(function() {
    var myClick = $('button').attr('onclick');
    $('button').removeAttr('onclick');

    $(document).on('click', 'button', function(e) {
        window.link_was_clicked=true;
        // do my other stuff here....
        eval(myClick);// this will now call setLocation()
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.