Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4091928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:25:40+00:00 2026-05-20T19:25:40+00:00

I have an online chat room that uses jQuery/Ajax for requesting data from the

  • 0

I have an online chat room that uses jQuery/Ajax for requesting data from the server. However, the method I use is very inefficient and I’m trying to improve it by only loading the server data if the chat content has been changed (new message, etc.) The method seems to be logically correct, although somehow it never evaluates to true. I’ve attached the code below, please tell me what I’m doing wrong as this is giving me a very hard time. I want the chat div to be changed only when the data from the server is different.

function loadMsgs()
{
var v_loadMsgs = new XMLHttpRequest();

v_loadMsgs.open("GET", "msgs.php");

v_loadMsgs.onreadystatechange = function()
{
    var old_content = $("#msgs").html();
    var new_content = v_loadMsgs.responseText;

    if (old_content != new_content) 
    {
        $("#msgs").html(new_content);
    }

    if (old_content == new_content) 
    {
        $("#msgs").html("EQUAL!"); // only here for testing
    }
};

v_loadMsgs.send(null);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:25:41+00:00Added an answer on May 20, 2026 at 7:25 pm

    I’d just use jQuery’s AJAX functions. They make everything really simple:

    function loadMsgs()
    {
        jQuery.get("msgs.php", function(data)
        {
            if ($("#msgs").html() != data) 
            {
                $("#msgs").html(data);
            } else {
                $("#msgs").html("EQUAL!"); // only here for testing
            }
        });
    }
    

    If you really want to know why your method isn’t working, you’re not checking if the server actually sent you a response (which returns a readyState of 4):

    function loadMsgs()
    {
    var v_loadMsgs = new XMLHttpRequest();
    
    v_loadMsgs.open("GET", "msgs.php");
    
    v_loadMsgs.onreadystatechange = function()
    {
        if (v_loadMsgs.readyState == 4)
        {
            var old_content = $("#msgs").html();
            var new_content = v_loadMsgs.responseText;
    
            if (old_content != new_content) 
            {
                $("#msgs").html(new_content);
            }
    
            if (old_content == new_content) 
            {
                $("#msgs").html("EQUAL!"); // only here for testing
            }
        }
    };
    
    v_loadMsgs.send(null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a chat program that displays I list of online users. The message
I have an iPhone chat app using XMPP with an EJABBERD server that is
I am writing an online chat and I have two objects, a chat room
I have following infrastructure and want to provide an online web chat (on server
We have online SharePoint site and we want to use tool or any way
I have an online text editor, very well made however it does not currently
I have an online service which uses a java applet to perform client side
I have looked online and i havent been able to fix a problem that
I have searched online for a solution, but have yet to find one that
I have something similar to an online chat client. When one user sends a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.