0

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

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

但是,如果我尝试检索 sessionId、userId 等的值,或者在按下 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);

    }

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

4

2 回答 2

1

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.

onbeforeunload 的 MSDN 描述

于 2012-11-16T12:19:56.160 回答
0

解决办法是,

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

于 2012-11-16T15:20:08.933 回答