【问题标题】:No need to close the window, If I press the cancel button in confirmation box?不需要关闭窗口,如果我在确认框中按取消按钮?
【发布时间】:2012-11-23 05:25:38
【问题描述】:

在我的聊天应用程序中,我需要一个确认框来在关闭窗口时注销。

确认框中的“确定”按钮可以正常工作。但是,

如果我按下,在确认框中取消,我不需要关闭浏览器窗口..

在我的情况下,如果我按取消,我的浏览器窗口已关闭...请帮助我...

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

        } 
    }
}

提前谢谢..

【问题讨论】:

    标签: java javascript ajax jsp servlets


    【解决方案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!";
    
    
      }
    

    };

    【讨论】:

      【解决方案2】:

      您无法阻止用户关闭窗口。这实际上是浏览器的一项功能,可防止恶意网站保持窗口打开。想想如果无法关闭窗口,弹出广告会发生什么。

      卸载处理程序可以防止用户导航离开,但不会关闭窗口。

      【讨论】:

      • 那如何防止窗口关闭?
      • 你不能。浏览器不会让您阻止用户关闭窗口。您通常可以做的最好的事情是在卸载时做一些事情来保存用户状态,这样他们就不会因为意外而丢失​​太多。
      • 如果我按取消,我不需要关闭窗口。如果我只按确定,我需要关闭窗口..我必须在哪里修改我的代码?请帮助我...它的紧急......
      猜你喜欢
      • 2020-08-17
      • 2013-12-30
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多