0

私のチャット アプリケーションでは、ウィンドウを閉じるときにログアウトするための確認ボックスが必要です。

確認ボックスの OK ボタンは問題なく動作しますが、

確認ボックスでキャンセルを押すと、ブラウザ ウィンドウを閉じる必要がなくなります。

私の場合、キャンセルを押すと、ブラウザ ウィンドウが閉じられました...助けてください...

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 サーブレットでは、

    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) {

        } 
    }
}

前もって感謝します..

4

2 に答える 2

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!";


  }

};

于 2012-11-22T21:25:38.437 に答える
0

ユーザーがウィンドウを閉じるのを防ぐことはできません。これは実際には、悪意のあるサイトがウィンドウを開いたままにしないようにするブラウザーの機能です。ウィンドウを閉じることができなかった場合、ポップアップ広告がどうなるかを考えてみてください。

アンロード ハンドラーは、ユーザーが別の場所に移動するのを防ぐことができますが、ウィンドウを閉じることはできません。

于 2012-11-16T09:27:54.967 に答える