Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Comparing string

    Hopefully someone here can clear this up. I've scoured the internet and can't for the life of me tell why this isn't working. All I'm trying to do is compare two strings in JavaScript. Here is the code I'm using.

    Code:
    document.getElementById(control_id).innerHTML=http_request.responseText;
    var strTemp = http_request.responseText.valueOf();
    var strReq = "InputValid";
    if(strReq.valueOf() == strTemp.valueOf()){
        alert("matches");
    } else {
        alert("does not match");
    }
    I've tried all kinds of variations of this, without the valueOf(), comparing the literal string "InputValid" to the variable, etc. No matter what I do the alert stating 'does not match' appears. Any thoughts?

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    document.getElementById(control_id).innerHTML=http_request.responseText;
    var strTemp = http_request.responseText;
    var strReq = "InputValid";
    alert('"' + strTemp + '"');
    if (strReq == strTemp) {
        alert("matches");
    } else {
        alert("does not match");
    }
    This should work assuming that http_request.responseText has the string "InputValid" in it. You can add the alert to check it. The valueOf() shouldn't be necessary at all.

    david_kw

  • #3
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the response. I took your suggestion and did some more investigation with it. Still doesn't work but I've got a little more information. First of all, it does work in IE I discovered. I've been mostly testing in the latest version of Firefox and there it still doesn't work. I have the exact code that you have above, here is the result I now get from the alert statement in Firefox.

    "
    InputValid"

    Exactly like that. So there is a " on one line, and then below it exactly is the rest of the string. Hence why its not matching in Firefox. Any ideas why this might be? Somehow Firefox seems to be adding a newline or something but IE doesn't. Suggestions are most welcome, thank you.

  • #4
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry to double post, but I think I've fixed it. I added this line

    Code:
    string = string.replace(/(^\s+|\s+$)/g,'');
    in order to remove spaces from the string and it seems to have worked. I'm not sure how a space gets in there in Firefox but not IE. If anyone has any thoughts on that I'd really appreciate it. Thank you.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    If your string is in fact a HTML code, well, yes, IE and Moz considers the "gaps" between the tags in different ways. Moz counts all the gaps, while IE counts only the "logical HTML possible" gaps, as possible textNodes. This might be the reason for that inconsistency.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •