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

前端 未结 9 1018
梦毁少年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: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.

提交回复
热议问题