I need to monitor when the user moves away from a page (to a different site not the same site) or closes the window/tab. I can achieve that with $(window).bind('unload', function() {});
but onload
is a catch-all event: it also catches page refresh and navigating to a different page on the same website. How can I detect those two events (page refresh and navigating to another page in the same website) using javascript/jQuery please?
6334 次
2 に答える
3
ありえないと思います、そんなイベント知りません。
考えられる解決策は、現在の URL を宛先 URL と比較することですが、セキュリティとプライバシーの理由からそれは不可能です。次を参照してください。
于 2012-09-17T14:15:56.187 に答える
2
各ページには、
var leaving = true;
$(function() {
$('a[rel!=ext]').click(function () { leaving = false; });
$('form').submit(function () { leaving = false; });
});
$(function() { window.onbeforeunload = unloadPage; });
function unloadPage() {
if(leaving) {
// Put your logic here
}
}
次に、外部サイトへのすべてのリンクにrel="ext"
属性があることを確認してください。
于 2012-09-20T16:12:03.617 に答える