0

I am currently using faye for pubsub and am disconnecting the client on the beforeunload event.While it disconnects during tab close during page refresh it throws the following error :

The connection was interrupted while the page was loading

The code is

window.addEventListener('beforeunload',function(event){
  fayeClient.disconnect();
  event.preventDefault();});

Is there a way to stop firefox from closing the connection before the call completes.The above code works perfectly in chrome

krishnan
  • 3
  • 1
  • On FF (not sure for lastest version), you could use a synchronous request. FYI, this won't work at all on chrome – A. Wolff Apr 30 '14 at 10:50
  • works fine on chrome 34 – Noino Apr 30 '14 at 10:53
  • @Noino Are you sure server will handle the request? AFAIK, chrome doesn't let ajax request to be send from onbeforeunload event (not tested on lastest chrome version) – A. Wolff Apr 30 '14 at 10:55
  • 1
    @A-Wolff tested just now before writing the suggestion to hack in my answer. – Noino Apr 30 '14 at 10:56
  • The library(faye) internally decides whether to use xhr or websockets.I am sure if i can make the call sync. – krishnan Apr 30 '14 at 10:56

1 Answers1

1

How can I prevent a page unload with jQuery?

way down in the comments it says:

event.preventDefault() doesn't work in this case, presumably because modern browsers don't want malicious coders to hijack the window and make it un-closable? – yochannah May 9 '13 at 8:45

I dont believe it is possible to delay it longer then it takes for that code to execute, excluding any async returns and timeouts.

So, to hack this. Call your disconnect, then make a synchronous call to a file which does

<?php
sleep(1);
Community
  • 1
  • 1
Noino
  • 593
  • 4
  • 13