Non-breaking space

This does not seem to work:

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

Is there something about inseparable space or ampersand that JavaScript doesn't like?

+52
javascript jquery
Mar 08 '11 at 20:32
source share
4 answers

  is an HTML object. When .text() is executed, all HTML objects are decoded to their character values.

Instead of comparing using an 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 a character from a character code manually in its irrevocable form:

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

Further information on String.fromCharCode is available here:

fromCharCode - MDC Document Center

More information about character codes for different encodings can be found here:

Windows-1252 Charset
UTF-8 Charset

+133
Mar 08 '11 at 20:35
source share

Remember that .text() shares the markup, so I don’t think you will find   as a result without markup.

Made in response ....

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

Shows a warning, because the equivalent of ASCII markup is returned instead.

+5
Mar 08 2018-11-11T00:
source share

This object is converted to char, which it represents when the browser displays the page. JS (jQuery) reads the displayed page, so it will not encounter such a text sequence. The only way he might come across is if you are double-encoded objects.

+2
Mar 08 '11 at 20:35
source share

JQuery docs for text() say

Due to differences in HTML parsers in different browsers, the return text may differ in new characters and other empty space.

Instead, I would use $td.html() .

+1
Mar 08 '11 at 20:40
source share



All Articles