Monday, March 17, 2008

XHTML Authentic

The XHTML has a foothold among developers, and real standards feel now practically obliged to use this language instead of HTML.

In most cases, however, XHTML is served as HTML. This article is born because many do not even know what we are talking about to program in XHTML not just enter the attributes quoted and add DOCTYPE right!

To get a true XHTML file, which is interpreted by the user agent as such (as XML, see "what XHTML?"), It is necessary to take some whose existence and need is ignored by most webmasters.

If serve as HTML not only deprive us of all the advantages of this language, but provide the user agent instructions wrong.For those who do consciously, because of support, and are actively planning using XHTML compatible with HTML, you can stop reading.

This article will show you who has never seen one of its pages truncated by an error caused by a '&' unencoded the true XHTML, but does not criticize those who have consciously chosen text / html.

MIME Type

I mentioned that quasi all sites programmed in XHTML (valid or not) are not actually true file XHTML. Most use dell'XHTML syntax, but are interpreted by the user-agent Communism as HTML files.And common thinking that if we specify the DOCTYPE for XHTML, our browser automatically understands that must interpret the code as such. Unfortunately, it is not at all the case.

When we ask a file server, it includes a MIME type in the answer. The MIME type tells the browser what sort of file server has returned.

The attribute application / zip, for example, tells the browser or user-agent that the file is a zip file, and this will decide what to do with

it. For a Zip file, it may take the row Winzip or a similar program and extract the files included in the archive. Other examples can be image / jpeg for a picture, or video / mpeg for a video.

HTML files are shipped with a MIME type text / html. This tells the browser that the file is an HTML file, and can begin to interpret the tags as usual: the problem is precisely that.

There is no reason why the XHTML should not have its MIME type, and in fact here here: application / xhtml + xml. If a file is sent as application / xhtml + xml, a browser that supports XHTML knows that he has been working with a file, and will use its XML parser to interpret the tags. This is the only way to interpret the code correctly.

Unfortunately, the files X HTML in 99% of cases with a MIME type text / html (and see why shortly). And 'why so many sites, though programmed in X HTML, are interpreted as common HTML. Nor. In reality as HTML incorrect, since the browser dell'HTMl follows the rules, but we have added extra bars quotes and unexpected.

Problems with MIME application / xhtml + xml

Unfortunately it is not so automatic browser to get to the right MIME type. If you have used the extension. Html for your files, this could be the reason why are interpreted as HTML even if the code is XHTML. The servers are almost all set to send the MIME type text / html if the extension is. Html,. Php or similar.

Indeed, the simplest way to resolve this problem would be to use the extension. Xhtml, but this may create problems for different types of browsers, especially if dated, and also some servers are not configured for this extension.

Specify the DOCTYPE for XHTML does not mean that the file is kind XHTML and not do anything with regard to the interpretation of user-agent. The meta-tag content type is equally useless, if the server sends a header that includes the MIME type of text-html!

So how can we do?Solutions

The only sure way to serve our file with the MIME type right, forcing the user-agent to interpret files as genuine XHTML, is swellhead intervene. There are several ways to do this. Let's look at some methods ...

Changing header with PHP

If our site is in PHP (or similar language) header can intervene in order to change it before the browser loads the page. The PHP code to be inserted use exactly to the top (otherwise mean that the browser has already started to load the page, and it will be too late):

No comments: