0

I would like to execute some code when a user refresh the page but Firefox won't execute my code and nothing work for me. I tried the example below which deoesn't work. I have a global variable called websocket (window.websocket).

window.onunload=function(){
    return window.websocket.send(JSON.stringify({type: 'disconnect'}));
};

$(window).on("unload",function() {
    return window.websocket.send(JSON.stringify({type: 'disconnect'}));
};

$(window).unload = function() {
    return window.websocket.send(JSON.stringify({type: 'disconnect'}));
};

window.addEventListener("unload", function (e) {
    window.websocket.send(JSON.stringify({type: 'disconnect'}));

    (e || window.event).returnValue = null;     //Gecko + IE
    return null;                                //Webkit, Safari, Chrome etc.
});

I've tried some more but nothing work in firefox expect the example below who show me a message come from nowhere and ask me to confirm exit page, but I don't want any confirmation. I really don't understand.

window.addEventListener("beforeunload", function (e) {
    var confirmationMessage = "\o/";

    (e || window.event).returnValue = confirmationMessage;     //Gecko + IE
    return confirmationMessage;                                //Webkit, Safari, Chrome etc.
});

I don't want any message, just run my code. It's impossible with Firefox?

X4V1
  • 75
  • 2
  • 9
  • Your last code example works in my version of Firefox (32.0.3), tested here: http://jsfiddle.net/xq9ofw7x/ – Niklas Oct 01 '14 at 21:07
  • yes but I don't want any message, I want to run my code when user exit, refresh or change page. I know it work but it's not what I ask. I would like to send a message to my server when user left. – X4V1 Oct 01 '14 at 21:12
  • window.addEventListener("unload", function (e) { window.websocket.send(JSON.stringify(something)); }); That is what I want but still don't work – X4V1 Oct 01 '14 at 21:14
  • 1
    Then you should show us the code that doesn't work... the one you showed so far works fine. – Niklas Oct 01 '14 at 21:14
  • I just want to run window.websocket.send(JSON.stringify({type: 'disconnect'})); when user refresh page. Is there any way to do that? – X4V1 Oct 01 '14 at 21:18
  • `window` does not have a `websocket` object out of the box. Did you add it somewhere else? – Niklas Oct 01 '14 at 21:32
  • Yes I added the object (It's a global variable) – X4V1 Oct 01 '14 at 21:33
  • It's because `WindowEventHandlers.onunload` is "...Not part of any standard.". Have a look here: http://stackoverflow.com/questions/9973816/why-is-jquery-unload-not-working-in-chrome-and-safari – algorhythm Oct 01 '14 at 21:40
  • window.onunload = function() { websocket.send(JSON.stringify({type: 'disconnect'})); return null; } This event is fired when I close the page but not when I refresh. Any idea? – X4V1 Oct 01 '14 at 21:48
  • Have you tried it with `{ return 'works!!!'; }` or `{ alert('works!!!'); }`? – algorhythm Oct 01 '14 at 22:09

1 Answers1

0

I found an semi working solution.

window.onbeforeunload = closeSocket;
function closeSocket(){
    window.websocket.send(JSON.stringify({type: 'disconnect'}));
    return false;
}

I have now to hide the confirm message and it's good. Any idea to hide the confirm message?

X4V1
  • 75
  • 2
  • 9