2

I have different xml's (for diff os), I am posting only relevant xml part for SunOs xml.

  <osname>SunOS
</osname>

This is fetched by jQuery

var osname = $(this).find('osname').text();

Later in the code when I compare, it always goes in to else part, I used console.info for firebug and attached a screenshot of my output

console.info("Before checking:"+osname);
if(osname=="HP-UX")
    console.info("HP-UX");
else if(osname=="AIX")
    console.info("AIX");
else if(osname=="SunOS")
    console.info("SunOS");
else
{
    console.info("Linux -");
    }

Screenshot of Firebug console.info

enter image description here

My question is why can't it check for SunOs or any other?

Note: I think there is a extra character after the osname, I also made a xsl in that when I check i do something like below, how can i do it in JS?

XSL code

<xsl:if test="$sunos='SunOS&#xa;'">

I have also tried if(osname=="SunOS&#xa;")

1
  • Thanks all for your answers :) Commented Jun 20, 2011 at 8:06

4 Answers 4

3

That's because the string is not actually SunOS, but SunOS\n (where \n stands for a newline). Use jQuery.trim to remove the surrounding spaces.

var osname = $.trim($(this).find('osname').text());
Sign up to request clarification or add additional context in comments.

Comments

3

May be you've got extraneous white space characters, try this

var osname = $.trim($(this).find('osname').text());

and compare using this osname. Check $.trim for more details.

Comments

1

There may be a lot of white-spaces appended to your osname-variable - I experience that quite often when using XML. Try checking on osname.trim().

Comments

1

It's probably because you get a line break at the end of the string, as the end tag is on the next line.

You can use the trim method to remove whitespace character around the string:

var osname = $.trim($(this).find('osname').text());

console.info("Before checking:"+osname);

switch (osname) {
case "HP-UX":
  console.info("HP-UX");
  break;
case "AIX":
  console.info("AIX");
  break;
case "SunOS":
  console.info("SunOS");
  break;
default:
  console.info("Linux -");
  break;
}

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.