简体   繁体   中英

How can I detect the event of FireFox closing?

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM