tags:

views:

146

answers:

4

I have been using prototype.js in a web application. I am populating some Divs dynamically, on selecting some radio buttons. Before populating contents in Div, I am clearing previous contents by using prototype's update method as -

$('item').update('');

In IE, this puts a line break automatically but not in Firefox 3.5. So, to work it in same manner as IE, I have change the code as -

$('item').update('&nbsp');

Now, this worked for me as expected. But, generally "&nbsp" is being used with a semicolon ($nbsp;). I want to know, if there can be a failure of this code. For example, is there any chances where &nbsp will be display instead of a blank space? But some browsers might be smart enough to detect the coding error by the programmer and autocorrect it.

+6  A: 

NEVER rely on browser autocorrection or "recommended" behavior.

If your code is wrong, then (a) it's wrong and (b) browsers are free to do inconsistent things with it.

S.Lott
If it is wrong, it should break somewhere.
See Gumbo's quote, this isn't auto-correction but defined behavior for SGML. But then you're relying on browsers correctly implementing the spec which is a futile thing to hope in general :-)
Joey
@Saurabh: When you can predict *all* future browser implementations, you'll see precisely where it will break. Until you can foresee *all* future browsers, you have to assume that wrong is wrong and will eventually break and cause you problems you did not foresee. Your test cases (IE and FireFox) doesn't even cover *all* current browser implementations. Don't hope wrong things will work. Just fix them.
S.Lott
+7  A: 

The ending semicolon can be omited but it’s absolutely not recommended. See this note in the HTML 4 specification regarding character references:

In SGML, it is possible to eliminate the final ";" after a character reference in some cases (e.g., at a line break or immediately before a tag). In other circumstances it may not be eliminated (e.g., in the middle of a word). We strongly suggest using the ";" in all cases to avoid problems with user agents that require this character to be present.

Gumbo
A: 

I concur, Dodgy markup and JavaScript errors are a disaster for debugging any problems you have in future as you can't eliminate the dodgy markup is causing the browser to interpret code incorrectly.

Sheff
A: 

not sure if this will help (or change anything) but the correct syntax is actually without the single quotes:

$('element').update();
seengee