0

I'm trying to compare a jQuery returned string (accessed by msg.d) and another string. This is currently what I have done, but it seems like it's not working and saying it's not equal every time:

setTimeout(ReloadChatMsgs, 1000);
function ReloadChatMsgs() {
    var msgs = document.getElementById("msgs_list");
    $.when($.ajax({
        type: "POST",
        url: 'Login.aspx/ReloadChatMsgs',
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msgs.innerHTML != msg.d.toString())
                msgs.innerHTML = msg.d;
        },
        failure: function (e) {

        }
    })).then(function () { setTimeout(ReloadChatMsgs, 1000); });
}
1
  • some error in console? msgs.innerHTML and msg.d are defined? are you sure that the output of msg.d.toString() are what you expected? Commented May 8, 2015 at 10:32

2 Answers 2

1

If you want to compare two strings irrespective of case

  1. Trim both the strings
  2. Convert both to lowercase

Code:

if ($.trim(msgs.innerHTML.toLowerCase()) != $.trim(msg.d.toString().toLowerCase()))
    msgs.innerHTML = msg.d

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

0

I just checked both values of msg.d and msgs.innerHTML and they are a bit different because msgs.innerHTML is already parsed to HTML codes (space for example). I resolved it by saving the last msg.d in a different var and used this to compare.

var last_msgs_set = "";
setTimeout(ReloadChatMsgs, 1000);
function ReloadChatMsgs() {
    var msgs = document.getElementById("msgs_list");
    $.when($.ajax({
        type: "POST",
        url: 'Login.aspx/ReloadChatMsgs',
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (last_msgs_set != msg.d) {
                msgs.innerHTML = msg.d;
                last_msgs_set = msg.d;
            }
        },
        failure: function (e) {

        }
    })).then(function () { setTimeout(ReloadChatMsgs, 1000); });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.