2

How to activate a function before user close browser/tab?

below is my ajax request

<script language="JavaScript" type="text/javascript">
 function srh_ajaxfunction_cny(id)
    {var xmlhttp;
    try{xmlhttp=new XMLHttpRequest();}
    catch (e){try{xmlhttp=new ActiveXObject("msxml2.xmlhttp");}
    catch (e){try{xmlhttp=new ActiveXObject("microsoft.xmlhttp");}
    catch (e){alert("Your browser does not support ajax!");      
    return false;}}}
    xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4){
        alert('done')
    }}  

        xmlhttp.open("get",url_path,true);
        xmlhttp.send(null); 
    }
</script>
user2097335
  • 65
  • 1
  • 3
  • 6

4 Answers4

11

Use beforeunload event. Normally, if there are asynchronous actions in that handler, browser won't wait for them to finish and will proceed with page unload (effectively interrupting your ajax call before it reaches your server).

There are some (hackish) tricks to make sure your ajax gets through before browser will unload page. One of those (used for example by services that track your behavior on webpages) is doing a loop, like so:

window.addEventListener('beforeunload', function() {
    // number of miliseconds to hold before unloading page
    var x = 200;
    var a = (new Date()).getTime() + x;

    // -----------
    // your ajax call goes here
    // -----------

    // browser will hold with unloading your page for X miliseconds, letting
    // your ajax call to finish
    while ((new Date()).getTime() < a) {}
}, false)
WTK
  • 16,583
  • 6
  • 35
  • 45
  • I need to work ajax only when browser is closed. this above code working when i refresh the page, in the hyperlinks, back and forward buttons of the browser – user2097335 Mar 25 '13 at 12:32
  • 1
    There's no way you can distinguish when browser is closing itself and when it just unloads page because user navigated (clicked link) to different page. From website point of view it's the same. – WTK Mar 25 '13 at 12:36
  • why i try to use this is to find make user online and offline. you have any other solution for this? – user2097335 Mar 25 '13 at 12:40
  • Of course, there are tons of way to do this, most of them involve holding information if user is online or offline on server side, plus (maybe) some javascript to send server information that given user is still online. Google it, one of the answers you might find helpful http://stackoverflow.com/questions/10559444/checking-if-specific-user-is-online-through-php-session-variable – WTK Mar 25 '13 at 12:51
  • Works as expected. Thanks – Venkatesh Manohar Feb 22 '18 at 06:20
  • wow, I'm really impress with while ((new Date()).getTime() < a) {} here. Can't belive this can hold browser from exiting. – TLVF2627 Mar 24 '22 at 16:22
0

use this function :

$(window).bind('beforeunload', function() {
    //do your stuffs
}); 
jishnu saha
  • 175
  • 7
0

bind it to onbeforeunload event

Anthony
  • 589
  • 3
  • 15
  • I need to work ajax only when browser is closed. this above code working when i refresh the page, in the hyperlinks, back and forward buttons of the browser – user2097335 Mar 25 '13 at 12:34
0

You can attach a function to window.onbeforeunload, but chrome wont allow you to execute any other code. You can only return a certain string which will be displayed before the user leaves.

svineet
  • 1,859
  • 1
  • 17
  • 28
  • I need to work ajax only when browser is closed. this above code working when i refresh the page, in the hyperlinks, back and forward buttons of the browser – user2097335 Mar 25 '13 at 12:35