Reputation: 115
I'm looking for a javascript function that executes when the user leaves the page, similar to the onbeforeunload
function, but I don't want the user notified with a dialog box
Upvotes: -2
Views: 121
Reputation: 1222
As David wrote, sending data to the server doesn't reliably work. However, you can attach an event handler to all links, prevent the default action, wait for the server response and then trigger the user's indented navigation. This is how Piwik and other statistics tools collect information.
For instance:
<script>
$('a').click(function() {
var my_href = $(this).attr('href');
$.ajax({url: 'http://some-url-here', success:function() {
location.href=my_href; // navigate now
}});
return false; // stop default navigation action
});
</script>
This is of course not triggered when the user just closes the tab, fills a form, types a new address etc.
Upvotes: 0
Reputation: 814
This performs an action but doesn't show a dialog box.
window.onbeforeunload = function(){
console.log('something');
}
Upvotes: 0