[英]How to logout during onbeforeunload/onunload using Javascript
<script type="text/javascript">
window.onbeforeunload=before;
window.onunload=after;
function before(evt)
{
var message="If you continue, your session will be logged out!";
if(typeof evt=="undefined"){
evt=window.event;
}
if(evt){
evt.returnValue=message;
}
}
function after()
{
var flex=document.${application}||window.${application};
flex.unloadMethod(); //calls the flex class containing the "unloadMethod()" which
//calls the logout.jsp that does the actually dropping of credentials
}
</script>
所以這就是我的想法:當用戶單擊,刷新或關閉窗口時,標准的onbeforeunload彈出框將與位於我的message變量中的文本一起出現。 然后,如果用戶單擊“取消”,則不會執行onunload事件,並且用戶將停留在當前頁面上。 但是,如果用戶單擊“確定”,則應觸發onunload事件,然后該事件將執行我的注銷類。
這就是發生的情況:需要時將顯示彈出窗口,“取消”按鈕也將起作用。 但是,如果用戶單擊“確定”,似乎onunload事件觸發得太快,我的注銷類無法執行,因為它永遠不會注銷它們。
我知道我的注銷類可以工作,因為如果我僅在onbeforeunload中調用flex.unloadMethod,它將注銷它們。
我已經研究了一個星期,看來我已經接近了,但是我把東西放錯了地方。 如果您有任何示例或建議,將不勝感激。
因此,我已經在Sunil D的幫助下解決了我的問題。onunload事件觸發得太快,無法觸發我的事件,因此我決定不包括警告消息,而只是注銷用戶一起使用onbeforeunload事件:
<script type="text/javascript">
window.onbeforeunload=before;
window.onunload=after;
function before(evt)
{
var flex=document.${application}||window.${application};
flex.unloadMethod(); //calls the flex class containing the "unloadMethod()" which
//calls the logout.jsp that does the actually dropping of credentials
}
function after(evt)
{
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.