[javascript] How is a non-breaking space represented in a JavaScript string?

This apparently is not working:

X = $td.text();
if (X == ' ') {
X = '';
}

Is there something about a non-breaking space or the ampersand that JavaScript doesn't like?

This question is related to javascript jquery

The answer is


The jQuery docs for text() says

Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.

I'd use $td.html() instead.


That entity is converted to the char it represents when the browser renders the page. JS (jQuery) reads the rendered page, thus it will not encounter such a text sequence. The only way it could encounter such a thing is if you're double encoding entities.


Remember that .text() strips out markup, thus I don't believe you're going to find   in a non-markup result.

Made in to an answer....

var p = $('<p>').html('&nbsp;');
if (p.text() == String.fromCharCode(160) && p.text() == '\xA0')
    alert('Character 160');

Shows an alert, as the ASCII equivalent of the markup is returned instead.