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

JavascriptJquery

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

  is a HTML entity. When doing .text(), all HTML entities are decoded to their character values.

Instead of comparing using the entity, compare using the actual raw character:

var x = td.text();
if (x == '\xa0') { // Non-breakable space is char 0xa0 (160 dec)
  x = '';
}

Or you can also create the character from the character code manually it in its Javascript escaped form:

var x = td.text();
if (x == String.fromCharCode(160)) { // Non-breakable space is char 160
  x = '';
}

More information about String.fromCharCode is available here:

> fromCharCode - MDC Doc Center

More information about character codes for different charsets are available here:

> Windows-1252 Charset
> UTF-8 Charset

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhillip SennView Question on Stackoverflow
Solution 1 - JavascriptAndrew MooreView Answer on Stackoverflow
Solution 2 - JavascriptBrad ChristieView Answer on Stackoverflow
Solution 3 - JavascriptJAAuldeView Answer on Stackoverflow
Solution 4 - JavascriptJacob MattisonView Answer on Stackoverflow