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
Javascript function that executes when leaving page without prompting user
119 Views Asked by jaydoggy At
2
There are 2 best solutions below
0
On
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.
This performs an action but doesn't show a dialog box.