Why your site most likely should not work

    On the Internet, you can easily find a lot of criticism of programmers who write bad code. There are many articles about bad and good programmers, and even special sites . But I am surprised by the almost complete indifference to that govnokod that typesetters write. In this article I will try to convey to you my thoughts on why the vast majority of sites on the Internet simply should not work.

    Fragment of example.com validation result


    As you know, self-aware one has the following opinion about validation:

    Лучший валидатор - это браузер

    This is a vivid example of amateurism and incompetence. This is the path by which, unfortunately, many studios work, and the name of this path is "Works, well, okay." An error-free layout in terms of validation should be as important for the site, as, in fact, program code.


    For some completely mysterious reason, the vast majority of sites are still in XHTML. But developers cannot answer the simple question “Why are you using XHTML?”. I have not heard a single intelligible explanation. And the reason is the usual misunderstanding of the subject.

    As you know, browsers always forgive mistakes in HTML, displaying the page "as is", even if you forget to close the tag. XHTML is XML-based, and XML is a strict markup language. This means that the parser simply will not be able to process invalid XML-code, giving a syntax error message.

    Check it out is very simple. It is enough to take any made-up "in accordance" with the standards XHTML-page and, having saved locally, rename it to * .xhtml. If after that the page opens in the browser without problems - I have good news for you. If not, you will probably be surprised at what you see.

    Screenshot of parsing error on habrahabr.ru main page. Firefox Browser

    “So if a page with invalid code opens locally with an error, then why don’t we see such errors on sites?” - the reader will justly ask.


    The DOCTYPE, which we indicate at the very beginning of the document, in fact, does not play any role in the choice of the parser by the browser.

    Each time the browser requests a page, the web server sends response headers before displaying the document itself. These headers are usually not visible, but you can view them using special development tools if you are interested. These headers are very important because they tell the browser how to interpret your document. The most important headline is Content-Type.
    Free translation from Dive into HTML5

    So, the browser parses the page in accordance with the HTTP response. But in it in most cases you can see: Content-Type: text/html;

    Screenshot of the HTTP response page obtained using the Web Developer Toolbar plugin for Firefox

    What does this mean? Upon receiving such an answer, the browser thinks it received HTML, not XHTML, and parses the code accordingly.

    That is why we do not see errors on sites. This is why you think that validation errors do not affect page display. Your site is simply deceiving the browser, and you yourself do not suspect it. Your site most likely should not open at all.

    If you want to get really what you wrote in your document, then the correct Content-Type should be like this:application/xhtml+xml

    So what?

    By declaring the XHTML th doctype at the very beginning of your page, and really using the XHTML syntax when writing the document, please check the code for validity and declare the correct content-type. And then get ready for surprises. You have to be sure that the visual editor in your CMS will return valid code (even if the client manually corrects the code in the html-view), you will have to worry about special characters , you will have to worry about CDATA, for example, in scripts, etc. Otherwise, the page just won't show up.

    XHTML is a bad idea. And it's good that they managed to understand this in the W3C, abandoning the development of XHTML 2.0 and transferring all their forces to HTML5.

    Choose the right technology, read the documentation and write the right code.

    Also popular now: