-8

Possible Duplicate:
javascript detect browser close tab/close browser

I need to save the current time into database when shutting down the browser. Does anyone have a solution to this problem?

thanks

Community
  • 1
  • 1
vinhquach
  • 9
  • 4
  • 1
    You'll want to send an AJAX request to the server in the `onbeforeunload` event and record it there. Millions of dupes on Stack Overflow, see http://stackoverflow.com/questions/3584288/can-the-unload-event-be-used-to-reliably-fire-ajax-request, http://stackoverflow.com/questions/9350900/sending-a-request-back-to-server-in-onbeforeunload-event or http://stackoverflow.com/questions/1821625/ajax-request-with-jquery-on-page-unload for starters. – Matt Jan 21 '13 at 09:44

3 Answers3

0

the unload event maybe work.

$(window).unload(function(){
  //write the time to database
});
0
window.onunload = function() {

var time = new Date().getTime();
var xhr = new XMLHttpRequest();
xhr.open("GET", "index.php?closetime="+time, true);
xhr.send();

};

In index.php use $_GET["closetime"] and save it to database.

-1

You can listen for the onbeforeunload event. You can make a call to the database when this event fires. Check out this article to learn how you might use this in a cross-browser fashion.

Kyle
  • 4,202
  • 1
  • 33
  • 41
  • 1
    "Making a call to the database" when the `onbeforeunload` event fires (assuming you're expecting him to use AJAX) is easier said than done. – Matt Jan 21 '13 at 09:45