window.onbeforeunload ajax request problem with Chrome

Posted by lang2 on Stack Overflow See other posts from Stack Overflow or by lang2
Published on 2011-02-09T14:08:15Z Indexed on 2011/02/09 15:25 UTC
Read the original article Hit count: 271

Filed under:
|
|

Hello,

I have a web page that handles remote control of a machine through Ajax. When user navigate away from the page, I'd like to automatically disconnect from the machine. So here is the code:

window.onbeforeunload = function () {
        bas_disconnect_only();
}

The disconnection function simply send a HTTP GET request to a PHP server side script, which does the actual work of disconnecting:

function bas_disconnect_only() {
   var xhr = bas_send_request("req=10", function() {
      }
      );
      }

This works fine in FireFox. But with Chrome, the ajax request is not sent at all. There is a unacceptable workaround: adding alert to the callback function:

 function bas_disconnect_only() {
   var xhr = bas_send_request("req=10", function() {
        alert("You're been automatically disconnected.");
      }
      );
      }

After adding the alert call, the request would be sent successfully. But as you can see, it's not really a work around at all.

Could somebody tell me if this is achievable with Chrome? What I'm doing looks completely legit to me.

Thanks,

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about AJAX