0

I'm trying to compare two strings and for some reason, I'm getting "false" as the result.

I've broken down the code to it's most simple function:

<script type="text/javascript">
function selectCat(cat) {
    var catName = cat.firstChild.nodeValue;
    alert(catName);
    if(catName.toString() == "Acronyms") {
        alert("True");
    } else {
        alert("False");
    }
}
</script>
</head>

<body>
<ul>
<li onclick="selectCat(this)">Acronyms</li>
</ul>
</body>

This works just fine in Dreamweaver's Live View, as well as in IE8 (when I press F12 to preview). However, when I upload this page to my company's web content manager (IBM WebSphere Portal), it no longer works.

I don't know what is different between the environments, but it's pretty frustrating. Anyone have any ideas why it might not be treating the category name as a String?

Gdubz
  • 61
  • 1
  • 10

2 Answers2

2

For some reason IE is including an extra space at the end of the nodeValue ie what you are actually getting is "Acronyms ".

Edit: To remove the trailing space you can add

if(catName.charAt(catName.length-1) == " "){
    catName = catName.substr(0,catName.length-1);
}
Ryan Erickson
  • 731
  • 1
  • 15
  • 23
  • This worked PERFECTLY, thank you sooo much Ryan! I had no idea that IE could do this/did this. – Gdubz May 25 '12 at 14:21
1

If IE is including an extra space at the end of nodeValue try -

var catName; 
catName = (catName.toString()).trim();

and then compare it with "Acronyms".

imhere
  • 4,405
  • 7
  • 26
  • 25
  • I didn't know that it might do that, I'll give it a try! – Gdubz May 25 '12 at 14:13
  • Tried it and for some reason, I get the following message in IE8... `Message: Object doesn't support this property or method` – Gdubz May 25 '12 at 14:20
  • 1
    Check this out - http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie – imhere May 25 '12 at 15:24