I want to notify the server on user closes browser window.
I tried all of the
$(window).bind("beforeunload", function() {
$.get("${contextPath}/notify?direction=logout");
});
and
$(window).unload( function() {
$.get("${contextPath}/notify?direction=logout");
});
and
$(window).unload(function() {
$.ajax({
url: "${contextPath}/notify?direction=logout"
});
});
but neither work well, despite the fact, that it is said in manual that it probably should.
In Firefox I have no notifications only on window close, but have ones on page refresh. In Chrome I have neither.
I tried to trace this script in Firebug or Chrome Developer Tools and found, that it is starting to work if traced! So I think it does not work normally because it has no time to send request before window closed or navigated.
Is this true? How to accomplish my task?
SOLUTION
This worked:
$(window).bind("beforeunload",
function() {
$.ajax({
async: false,
url: 'notify'
});
}
);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…