Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does HTML and CSS needs to be valid before we apply JavaScript?

I was thinking about Web standards, and i was wondering if we need to have valid HTML and CSS before we apply JavaScript?

Can anyone enlighten me on the relation between valid HTML, CSS and JavaScript?

For instance, does invalid HTML and CSS prevent JavaScript from working correctly?

like image 203
HTMLCSS_noob Avatar asked Mar 07 '10 15:03

HTMLCSS_noob


2 Answers

As much lip service as we pay to having valid HTML, browsers are extremely forgiving. The reasons for this are historical. There was (and still is) a lot of bad HTML in the beginning.

But you may get unpredictable differences between browsers with badly formed HTML. For example, different browsers may treat an unclosed tag differently.

All the markup causes to happen is to form a DOM (Document Object Model) in the browser based on how that browser interpreted it. CSS and Javascript are both applied to the DOM.

like image 87
cletus Avatar answered Sep 29 '22 00:09

cletus


I disagree with the above responses: I have had very bad times hunting through apparent javascript bugs before I remembered to validate the page I was working on. As soon as I corrected the glaring errors, the scripts worked as intended.

The most obvious problem (not one I have experienced, because I already look for that): have two elements with the same id attribute, and try to make your script that depends on that to work as intended :)

While it may be true that the level of validation needed is somewhat low, when you are banging your head against the table because your website doesn't work and you don't know why, you'll remember this fondly.

like image 36
Adriano Varoli Piazza Avatar answered Sep 28 '22 22:09

Adriano Varoli Piazza