Search code examples
javascriptjqueryhtmlkeyboardreloading

I am trying to create a chat feature for a web page and it keeps reloading when I hit enter. Can anyone tell me why this might be happening?


This is the main body of the code. Basically I am trying to write the client side page for a chat server. Assuming the server interaction is correct the page will never be able to take the messages sent back from the server because the page refreshes every time I hit the enter. The code is supposed to send a message to the server and then the server sends the message to all connected clients and the client appends the message to the end of the list.

<!DOCTYPE html>
    <html>
    <head>
    <title>Chat</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="http://localhost:4000/static/_stylesGlobal.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>

        function sendChat(msg)
        {
            socket.emit('newMessage', {message:msg} );
            console.log("Sent by User: %s", msg);

        }

        function updateChatLog(msg)
        {
            $("#chatLog").append(msg.message + "<br>");
            console.log(msg.message);
        }

        function msgReceived(msg)
        { 
            $("clientCount").html(msg.clients);
        }

        function clientUpdate (msg)
        {
            $("#clientCount").text(msg.clients);
        }


         $(document).ready(function () {

            $("#clientCount").text("0");


            socket = io.connect('http://localhost:4000');

            socket.on('mUpdate',function(msg){updateChatLog(msg); msgReceived(msg);});
            socket.on('nClientUpdate',function(msg){clientUpdate(msg);});
            $("#enterChat").click( function() {
            var messageValue = $("#chatMessage").val();
            console.log("Entered by User: %s", messageValue);
            sendChat(messageValue);

            });

            });




    </script>   
    </head>


    <body>
    <header role="banner" >
        <h1>Chat</h1>

        </header>
    <nav role="navigation" id = "main_nav_bar">
    <ul>
        <li id="homenav"><a href="http://localhost:4000/">Home</a></li>
        <li id="calculatornav"><a href="http://localhost:4000/static/calculator.html">Calculator</a></li>
        <li id="logbooknav"><a href="http://localhost:4000/static/logbook.html">Logbook</a></li>
        <li id="gallerynav"><a href="http://localhost:4000/static/gallery.html">Gallery</a></li>
         <li id="downloadnav"><a href="http://localhost:4000/static/download.html">Download</a></li>
        <li id="chatnav"><a href="http://localhost:4000/static/chat.html">Chat</a></li>
    </ul>
    </nav>

        <main>
            <div id="chatWindow">
            <h3>Welcome To Chat</h3>


            <div id="chatLog"></div>

            </div>
    <form id="chatInput">
    <p><span id="clientCount">0</span> Online Now</p>
    <br>
      <input type="text" id="chatMessage" value="Your Message Here !" style="width: 100%;">
      <br>
      <input type="submit" value="Enter" href="#chatInput" id="enterChat">
    </form> 

        </main>

    <footer role="contentinfo">
    </footer>



    </body>
    </html>

The main culprit for this reload is the enter chat function:

$("#enterChat").click( function() {
            var messageValue = $("#chatMessage").val();
            console.log("Entered by User: %s", messageValue);
            sendChat(messageValue);

            });

If anyone could point me towards why the page is reloading I could actually work towards seeing if the message from the server is being passed properly. On top of that I would really appreciate it.


Solution

  • You can just add this

    $('#chatInput').submit(function(e) {e.preventDefault()});
    

    To prevent the default behavior of form submission on enter.