PHPまたはJavascriptを使用してブラウザでタブクローズを検出する方法。つまり、ページが更新されたか、新しいタブで開かれたかを確認する方法です。私はブラウザではなくタブに関心があります。
2 に答える
window.onbeforeunloadイベントのリスナーを持つことができます。ただし、JavaScriptでタブが閉じられているかどうかを検出することはできません。
実際、Javascriptは、タブが閉じられるかどうかを教えてくれます。2つの異なる方法を使用する必要があります(1つはIE用、もう1つは他のすべての人用)。
私はあなたが求めていることを実行するためにJavascriptプロセスを作成しました。プロセスの前提条件は、jQueryと1つのプラグインです(livequery-一部のHTMLはページの読み込み後に動的に生成されるため)。とにかく、これがすべてこれを行うjsファイル(checkclosing.js)です:
// Global Var
var bodyclicked = false;
var datachanged = false;
var nodirtycheck = false;
// Start the engines :)
$(document).ready(function () {
init();
});
function init() {
bindEvents();
}
function bindEvents() {
// Bind the onClick event for the DOM body
$(document).livequery(function () {
bodyclicked = true;
});
// Bind our event handlers for the change and reset events
$(':input').not('.excludeFromDirtyCheck').bind('change', function () {
if ($(this).hasClass('dataLoader')) {
if (!datachanged) {
return;
}
}
datachanged = true;
$(".hidDataChanged").val("True");
});
$(':reset,:submit').bind('click', function () {
// .NET renders some ASP Buttons as Submit and Reset types
if ($(this).hasClass('notSubmit')) {
return;
}
if ($(this).hasClass('notReset')) {
return;
}
datachanged = false;
$(".hidDataChanged").val("False");
});
// Must have the livequery plugin referenced for this to work
$('.resetchangedform').livequery('click', function (event) {
//alert("resetchanged"); // FIXME
datachanged = false;
// Set our hidden input (on the Administration Master page)
$(".hidDataChanged").val("False");
});
// Must have the livequery plugin referenced for this to work
$('.setchangedform').livequery('click', function (event) {
//alert("setchanged"); // FIXME
datachanged = true;
// Set our hidden input (on the Administration Master page)
$(".hidDataChanged").val("True");
});
// Must have the livequery plugin referenced for this to work
$('.excludeFromDirtyCheck').livequery('click', function (event) {
nodirtycheck = true;
});
// Must have the livequery plugin referenced for this to work
$('.notSubmit').livequery('click', function (event) {
nodirtycheck = true;
});
// Must have the livequery plugin referenced for this to work
$('.dataReloader').livequery('change', function (event) {
nodirtycheck = true;
});
window.onbeforeunload = function () {
//alert("datachanged = " + datachanged + " | nodirtycheck = " + nodirtycheck + " | hidDataChanged = " + $(".hidDataChanged").val());
// Check the hidden textbox
if ($(".hidDataChanged").val() == "True") {
datachanged = true;
}
if (nodirtycheck) {
nodirtycheck = false;
}
else {
if (navigator.appName == "Microsoft Internet Explorer") {
// IE
if (bodyclicked) {
if (datachanged) {
return "You may have unsaved changes...";
}
}
else {
bodyclicked = false;
// Do Nothing
}
}
else {
// Not IE
if (datachanged) {
return "You may have unsaved changes...";
}
else {
// Do Nothing
}
} //if (navigator.appName == "Microsoft Internet Explorer") {
} //window.onbeforeunload = function () {
} // if (nodirtycheck) {
}
次に、閉じるタブを確認するページにこのファイルへの参照を含めるだけです。
これは、フォーム値に未保存の変更があるページからユーザーが移動できないようにするために作成されました。はい、ほとんどの場合、ユーザーがタブを閉じたり移動したりできないようにするのは悪い習慣ですが、この場合、ユーザーはそれを要求しました。これは一般向けではない内部アプリケーションです。
ユーザーがタブを閉じたり移動したりする前に変更をチェックしたくないコントロールには、excludeFromDirtyCheckというクラス名が付けられます。
これは必要以上のことかもしれませんが、役に立たない部分を取り除くことができます。基本的な前提は同じです。