issue on Google Chrome??

Discussion in 'JavaScript' started by lp1051, Mar 12, 2009.

  1. #1
    Hi,

    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: http://groups.google.com/group/goog...g/browse_thread/thread/7a7071a886163145?pli=1
    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')
    </script>
    
    Code (markup):
    Btw. the same behavior is repro using & # 160;

    Thanks for any ideas, advices.
     
    lp1051, Mar 12, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    I got answer on SitePoint, Google Chrome v.2 doesn't have this problem anymore, so just in the case you will need to evaluate &nbsp;, for compatibility remember charCodeAt()
     
    lp1051, Mar 12, 2009 IP