JavaScript, browsers, window close - send an AJAX request or run a script on window closing

前端 未结 9 860
梦毁少年i
梦毁少年i 2020-11-22 04:40

I\'m trying to find out when a user left a specified page. There is no problem finding out when he used a link inside the page to navigate away but I kind of need to mark up

相关标签:
9条回答
  • 2020-11-22 05:18

    Use:

    <body onUnload="javascript:">
    

    It should capture everything except shutting down the browser program.

    0 讨论(0)
  • 2020-11-22 05:20

    Im agree with Felix idea and I have solved my problem with that solution and now I wanna to clear the Server Side solution:

    1.send a request from client side to server

    2.save time of the last request recived in a variable

    3.check the server time and compare it by the variable of last recived request

    4.if the result is more than the time you expect,start running the code you want to run when windows closed...

    0 讨论(0)
  • 2020-11-22 05:22

    There are unload and beforeunload javascript events, but these are not reliable for an Ajax request (it is not guaranteed that a request initiated in one of these events will reach the server).

    Therefore, doing this is highly not recommended, and you should look for an alternative.

    If you definitely need this, consider a "ping"-style solution. Send a request every minute basically telling the server "I'm still here". Then, if the server doesn't receive such a request for more than two minutes (you have to take into account latencies etc.), you consider the client offline.


    Another solution would be to use unload or beforeunload to do a Sjax request (Synchronous JavaScript And XML), but this is completely not recommended. Doing this will basically freeze the user's browser until the request is complete, which they will not like (even if the request takes little time).

    0 讨论(0)
  • 2020-11-22 05:26

    Years after posting the question I made a way better implementation including nodejs and socket.io (https://socket.io) (you can use any kind of socket for that matter but that was my personal choice).

    Basically I open up a connection with the client, and when it hangs up I just save data / do whatever I need. Obviously this cannot be use to show anything / redirect the client (since you are doing it server side), but is what I actually needed back then.

     io.on('connection', function(socket){
          socket.on('disconnect', function(){
              // Do stuff here
          });
     });
    

    So... nowadays I think this would be a better (although harder to implement because you need node, socket, etc., but is not that hard; should take like 30 min or so if you do it first time) approach than the unload version.

    0 讨论(0)
  • 2020-11-22 05:27

    I also wanted to achieve the same functionality & came across this answer from Felix(it is not guaranteed that a request initiated in one of these events will reach the server).

    To make the request reach to the server we tried below code:-

    onbeforeunload = function() {
    
        //Your code goes here.
    
        return "";
    } 
    

    We are using IE browser & now when user closes the browser then he gets the confirmation dialogue because of return ""; & waits for user's confirmation & this waiting time makes the request to reach the server.

    0 讨论(0)
  • 2020-11-22 05:31

    Basing this on other answer and this blog post.

    To make things clear, in 2018 (still today 2020), Beacon API is the solution to this issue (on almost every browser)

    Beacon Requests are guaranteed to be initiated before a page is unloaded and they are run to completion, without requiring a blocking request.

    You can use it inside onunload event, and be confident it will be sent.

    $(window).on('unload', function() {
        
        var URL = "https://example.com/foo";
        var data = "bar";
    
        navigator.sendBeacon(URL, data);
    
    });
    
    0 讨论(0)
提交回复
热议问题