2012-11-16 41 views
0

在我的聊天應用程序,我需要一個確認框關閉窗口時註銷。無需關閉該窗口,如果我按在確認框中取消按鈕?

在確認對話框OK按鈕的使用fine.but,

如果我按,取消確認框,我不需要關閉瀏覽器窗口..

就我而言,如果我按取消,我的瀏覽器窗口被關閉了......請幫我...

window.onunload = function() { 

    var confirmation = confirm("Are you Sur want to logout the session ?"); 
    if (confirmation == true) 
     { 
     if((sessionId != null)&&(sessionId!="null")&& (sessionId != "")) 
      logout(); 
    // confirmation = "You pressed OK!"; 
     } 
    else 
     { 
    // confirmation = "You pressed Cancel!"; 


     } 
}; 

在註銷代碼,

function logout(){ 
    //alert("<---------->"+userId+";"+secureKey+";"+name); 
    clearInterval(timer); 
    document.getElementById("button3").style.display = "none"; 
    document.getElementById("message").innerText = ""; 
    try 
    { 
     xmlhttp.onreadystatechange=function() 
     { 
      //alert("Status : "+xmlhttp.status+"\nreadyState : "+xmlhttp.readyState); 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       //alert("<---------->"+userId+";"+secureKey+";"+name); 
       //alert(xmlhttp.responseText.toString()); 
      } 
     }; 
     xmlhttp.open("POST","LogoutAction?&userId="+userId+"&secureKey="+secureKey+"&nickname="+name,true); 
     xmlhttp.send(); 
    } 
    catch(err) 
    { 
     alert(err.description); 
    } 
} 

在LogoutAction的Servlet,

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 

     secureKey = request.getParameter("secureKey"); 
     userId = request.getParameter("userId"); 
     //nickname = request.getParameter(nickname); 
     protocol = ApplicationInfo.flexProtocol; 

     logout = new Logout(); 
     logout.requestLogout(secureKey, userId, null, protocol); 
     //out.println(secureKey+";"+userId+";"+nickname); 
    } 

在Java代碼中,

public class Logout { 
public void requestLogout(String secureKey, String userId, String nickname, FlexChatProtocol protocol) { 

     RequestLogout logout = null; 

     Message resp = null; 
     logout = RequestLogout.create(); 
     logout.setSecureKey(secureKey); 
     logout.setUserId(userId); 
      try { 

      resp = protocol.request(logout); 
      System.out.println(resp); 


     } catch (ProtocolException e) { 

     } catch (IllegalStateException e) { 

     } 
    } 
} 

在此先感謝..

回答

1

你應該使用onbeforeunload ..這樣的:

window.onbeforeunload = function() { 

var confirmation = confirm("Are you Sur want to logout the session ?"); 
if (confirmation == true) 
    { 
    if((sessionId != null)&&(sessionId!="null")&& (sessionId != "")) 
     logout(); 
// confirmation = "You pressed OK!"; 
    } 
else 
    { 
// confirmation = "You pressed Cancel!"; 


    } 

};

0

您不能防止用戶關閉窗口。這實際上是瀏覽器的一項功能,可以防止惡意網站打開窗口。想到將與彈出式廣告發生什麼,如果你不能關閉該窗口。

卸載處理程序可以防止用戶導航走,但不關閉窗口。

+0

那麼如何防止窗口的關閉? –

+0

你不能。瀏覽器不會讓你阻止用戶關閉窗口。你通常可以做的最好的就是做卸載,保存用戶的狀態東西,所以他們並沒有失去太多,如果它是偶然的。 – EdC

+0

如果我按取消我不需要關閉窗口。如果我只按下確定,我需要關閉窗口..在哪裏我必須修改我的代碼?請幫助我......緊急...... –