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

  • Home
  • SEARCH
  • 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 personal project that's been online for sometime now. I've been keeping
I have seen simple example Ajax source codes in many online tutorials. What I
I have something similar to an online chat client. When one user sends a
I have been working on on an AJAX chat application using php, mysql. It's
I have a online PHP application to which I want that the Admin should
This is for homework but please know that I have looked online for help
I have a simple chat client set up that allows users to login with
Let's say I have I have an online store with a shopping cart feature
I've done a bit of functional programming, and I have a couple online references,
I have to choose an online WYSIWYG editor. I'm pending between TinyMCE and Xinha

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.