开发者

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 va开发者_如何学Clid HTML, CSS and JavaScript?

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜