solved:
$(window).on('beforeunload', function() {
$.ajax({
type: "POST",
async: false,
cache: false,
url: "myScript.php",
data: ({ /* my data */ }),
success: function(result) { /* do nothing */ },
error: function(data) { location.reload(); }
});
});
$(window).onbeforeunload(...); didn't work.