每次都收到离开页面警报,当我按下按钮在客户端聊天的 servlet 中使用 ajax 检索消息和值时



在我的jsp页面中,我显示了包含文本区域,文本字段和发送按钮的div,只有当我得到sessionId!="null"时。

此外,我有一个确认警报框,用于关闭窗口以注销会话...

但是如果我尝试检索会话 Id、用户 ID 等的值或在按下 startChat 或发送按钮时将消息发送到服务器,我每次都会收到相同的警报......在我的 Jsp 页面中,

<div id="login">
<form id="indexForm" name="indexForm" action="LoginAction" method="post">
<table>
    <tr>
        <td id="fname" class="fontStyle">First Name :</td>
        <td id="fvalue"><input type="text" id="firstname"
            name="firstname" /></td>
        <td id="lname" class="fontStyle">Last Name :</td>
        <td id="lvalue"><input type="text" id="lastname" name="lastname" /></td>
    </tr>
    <tr>
        <td colspan="4" id="err1" style="color: red">&nbsp;</td>
    </tr>
    <tr>
        <td id="email_id" class="fontStyle">Email Id&nbsp;&nbsp; &nbsp;:</td>
        <td><input type="text" id="email" name="email" /></td>
        <td id="sub" class="fontStyle">Subject
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</td>
        <td><input type="text" id="subject" name="subject" /></td>
    </tr>
    <tr>
        <td colspan="4">&nbsp;</td>
    </tr>
    <tr>
        <td colspan="4" id="err2" style="color: red">&nbsp;</td>
    </tr>
    <tr>
        <td colspan="4">&nbsp;</td>
    </tr>
    <tr>
        <td></td>
        <td>
        <center><a id="button" href="javascript:getValues();">Start
        chat</a></center>
        </td>
        <td>
        <center><a id="button" href="javascript:logout();">Stop
        chat</a></center>
        </td>
        <td></td>
    </tr>
</table>
</form>
</div>
<div style="display: none;" id="div">
<div><textarea id="textarea" rows="10"></textarea></div>
<div>
<table>
    <tr>
        <td id="msg" class="fontStyle">message :</td>
        <td id="msgvalue"><input size="40" type="text" id="message"
            onkeydown="enterKey()" /></td>
        <td style="width: 5%;"></td>
        <td><!-- <input type="button" value="send" onclick="sendMessage()" /> -->
        <a id="button3" href="javascript:sendMessage();">Send</a></td>
    </tr>
</table>
</div>
</div>

在我的JavaScript中,

window.onbeforeunload = function () {
      return  "Are you Sure want to LOGOUT the session ?";
};
window.onunload = function () {
        if((sessionId != null)&&(sessionId!="null")&& (sessionId != ""))
            logout();
};
function sendMessage(){
    message = document.getElementById("message").value;
    document.getElementById("message").innerText = "";
    try
    {
        xmlhttp.onreadystatechange=function()
        {
            //alert("Status : "+xmlhttp.status+"nreadyState : "+xmlhttp.readyState);
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {

                var checkMsg = decodeURIComponent(xmlhttp.responseText.toString());
                if(checkMsg != "null" && checkMsg != null){
                    //document.getElementById("textarea").innerHTML +=  checkMsg;
                    if(textarea.value == "")
                        textarea.value = checkMsg;
                    else
                        textarea.value += "n"+ checkMsg  ;
                }
            }
        };
        xmlhttp.open("POST","SendMessageAction?sessionId="+sessionId+"&userId="+userId+"&securekey="+secureKey+"&message="+encodeURIComponent(message)+"&sid"+Math.random(),true);
        xmlhttp.send();
    }
    catch(err)
    {
        alert(err.description);
    }
}

在我的 Servlet 中,

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        msg = request.getParameter("message");
        //System.out.println("msg -----> : "+msg);
        seckey = request.getParameter("securekey");
        uid = request.getParameter("userId");
        sessionId = request.getParameter("sessionId");
        //counter =Integer.parseInt(request.getParameter("counter"));
        counter = 1;
        protocol = ApplicationInfo.flexProtocol;
        message = new SendMessage();
        message.send(msg, seckey, uid, sessionId, counter, protocol);
        CustomerInfo customer = ApplicationInfo.customerDetails.get(uid);
        out.print(customer.getMessage());
        //System.out.println("msgDecoded -----> : "+msg);
    }

帮帮我。。。我不需要每次都收到警报,除了我正在关闭浏览窗口...

当我们执行以下操作之一时,将触发事件onbeforeunload

Close the current window.
Navigate to another location by entering a new address or selecting a Favorite.
Click an anchor that refers to another document.
Invoke the anchor.click method.
Invoke the document.write method.
Invoke the document.close method.
Invoke the window.close method.
Invoke the window.navigate or NavigateAndFind method.
Invoke the location.replace method.
Invoke the location.reload method.
Specify a new value for the location.href property.
Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
Invoke the window.open method, providing the possible value _self for the window name.
Invoke the document.open method.
Click the Back, Forward, Refresh, or Home button.

MSDN 对卸载前的

描述

解决方案是,

我在 java 脚本中为事件使用锚标记而不是按钮。所以 "window.onbeforeunload " 将事件视为一个新地址。现在我将锚标签更改为按钮并且工作正常。

最新更新