現在、サイトのユーザーがウィンドウ/タブを閉じたり、URL を変更したりしたかどうかを検出しています。
完全に機能する次のコードを使用しています。
var validNavigation = false;
function endSession() {
// Browser or broswer tab is closed
// Do sth here ...
alert("bye");
}
function wireUpEvents() {
window.onbeforeunload = function() {
if (!validNavigation) {
endSession();
}
}
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
$(document).ready(function() {
wireUpEvents();
});
私の問題は、イベントをアラートからオーバーレイに変更したいということです。非表示の div オーバーレイをセットアップし、上記のコードのアラートを次のように置き換えました。
$('.overlay').css('display','block');
ユーザーをページにとどまらせるものが div 内にないため、これは機能しなくなりました。(ユーザーはアラート内のボタンをクリックする必要はありません)
これを回避する方法について何か提案はありますか?
乾杯、ダン