我嘗試使用onbeforeunload和Unload函數。但它沒有奏效。點擊鏈接或刷新時,此事件被觸發。我想要一個只在瀏覽器窗口或標籤頁關閉時觸發的事件。該代碼必須適用於所有瀏覽器。如何檢測瀏覽器窗口/選項卡關閉事件?
我在主頁面使用以下代碼。
<script type="text/jscript">
var leaving = true;
var isClose = false;
function EndChatSession() {
var request = GetRequest();
request.open("GET", "../Chat.aspx", true);
request.send();
}
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;
if (keycode == 116) {
isClose = true;
}
}
function somefunction() {
isClose = true;
}
window.onbeforeunload = function (e) {
if (!e) e = event;
if (leaving) {
EndChatSession();
e.returnValue = "Are You Sure";
}
}
function GetRequest() {
var xmlHttp = null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
//Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
,並在我的身體標記:
上面的代碼在IE,但不是在鉻...
在刷新時也會調用警報。 。 。 。任何想法排除刷新事件 – Gora