onbeforeunload를 사용하여 Ajax 요청으로 변수를 재설정합니다. 그러나 한 번만 실행됩니다. 예를 들어 (로그인 한 후) 페이지를 방문하여 창을 닫으면 기능이 실행되지만 작업을 반복하면 (브라우저에서 로그인을 완료하고 창을 닫을 때) 주소가 반복됩니다.onbeforeunload가 한 번만 실행됩니다.
window.onbeforeunload = function (e) {
//Ajax request to reset variable in database
resetDemoValueWithAjax();
};
//Ajax request to reset variable in database and exit
function resetDemoValueWithAjax(){
$.ajax({
async:true,
type: "POST",
dataType: "json",
url:"http://localhost/site/index.php/welcome/resetDemoValue",
data: {name: "demo"},
success:function(){
document.location.href='http://localhost/site/index.php/auth/logout';
},
error: function(){
document.location.href='http://localhost/site/index.php/auth/logout';
}
});
}
document.location.href='http://localhost/site/index.php/auth/logout';
은 다른 순간에 사용됩니다 사용자가 로그 아웃 않는 경우. 여기가 아닙니다
명확하고 간결합니다. 감사. – vicenrele