issue on Google Chrome?


during fixing one xhtml page, I found very strange behavior of Google Chrome.
There was a placeholder div containing ’ ’ only.
While IE, FF, Opera, Safari gave value of innerHTML as a string (’ '), Chrome returned space. That would be OK, but the strange thing is that it is not ordinary space. I mean you cannot use if(the_div.innerHTML == " "), it simply won’t evaluate this condition as true. The only way I found around was to use charCodeAt() and check if it’s equal 160.

Maybe I missed something, that’s why I’m asking here if you experienced similar problem, or found some explanation. Is it bug on Chrome, or just design.
I also found similar problem here:
but with no reply…

Not sure if it’s clear what I mean, so here is very basic code sample :

<div id="d">&nbsp;</div>

<script type="text/javascript">
var cont=document.getElementById('d').innerHTML;
if(cont == '&nbsp;') alert('nbsp');
if(cont == ' ') alert('space');
if(cont.charCodeAt() == 160) alert('charCodeAt')

Btw. the same behavior is repro using & # 160;

Thanks for any ideas, advices.

Here’s a silly question. Why don’t you just place a space inside of the div?

:slight_smile: why am I asking about strange ’ ’ interpretation in JS in Chrome and not about other ways around?
Anyway, thanks for an idea

I suspect that Google Chrome is using the correct Unicode character for a non-breaking space.

You can use the charCodeAt method to find out the character code of what’s being used.

so would you suggest to report it as a Chrome bug, or let it be??

I have just run your test code, and in Google Chrome as well as Firefox and Internet Explorer, I am having alerted ‘nbsp’

My other machine is running version 1 of Google Chrome and reports ‘charCodeAt’

This is because I’m running version 2 of Google Chrome, so it seems that the bug has already been fixed.

Ok, thanks a lot! I’m running v.1 (latest though). Good to save them work by closing bug report.
Still don’t understand why the space was acting like it did, but now it doesn’t matter…
Thanks again

It’s because it’s not actually a space.   is a code for an entity called the non-breaking space. I suspect that Google Chrome was initially taking these codes, like > and " and interpreting them to their actual characters when the HTML is interpreted to create the DOM.

It’s the DOM that you actually work with from JavaScript, not the original HTML, so that’s likely to be the cause.