I have a site which sits within an iframe.
How can I detect and record the time that a user navigates away from my site?
I started doing this:
$(window).unload(function() {
$.post('record_action.php');
});
but what was happpening was that the ajax function fired, but did not complete because the page navigated away and the ajax function ended early (did not connect properly)
Because AJAX calls are asynchronous by default, before your
$.postis called, the page is closed and no result is sent to your server.You need to use synchronous ajax call here to make sure the page closes after the post submission.