What is the difference between .html and .xhtml ex

2019-04-07 12:21发布

What is the difference between the .html and .xhtml file extensions?

What is the benefit of using the .xhtml extension?

Why we are not using the .xhtml extension, is it just because of IE?

What about .xhtml extension with IE 8?

Is .xhtml supported in other browsers besides IE? If yes, then what benefit we will get when all browsers support the .xhtml extension. Will we stop using .html?

Why do we use the XHTML doctype, but save those files using the .html extension?

6条回答
乱世女痞
2楼-- · 2019-04-07 12:34

Browsers doesn't rely (just) on page extension, but in MIME content-type. This way you can, for instance, to create a PHP page which serves a JPG image.

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-07 12:35

Nobody cares what the file is named, just what the MIME type it's been transferred with is.

查看更多
别忘想泡老子
4楼-- · 2019-04-07 12:36

If served over HTTP, the file extension has no meaning. The only information that matters it the Content-Type header field where the media type of the resource is specified.

But when served from a local filesystem, the media type is normally identified by the file extension.


Edit    I think the reason for why the extension .html is used even if it’s XHTML is because XHTML is HTML just with XML syntax and everyone is used to .html for HTML documents. (Although most XHTML documents are actually served as HTML as the media type text/html denotes HTML no matter what the document type declaration says.)

But again: extensions are not necessary when requested over HTTP. In HTTP the Content-Type header field tells what media type the resource should be interpreted with. So in theory you could use whatever extension you want or even use no extension at all (useful when content negotiation is used).

查看更多
5楼-- · 2019-04-07 12:37

I think the xhtml extension is used for XHTML only, but it is rarely used, because the HTML-type you are using is defined in the DOCTYPE-element.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
查看更多
\"骚年 ilove
6楼-- · 2019-04-07 12:45

Use the extension which matches the MIME type:

  • .xhtml is for application/xhtml+xml documents
  • .html is for text/html documents

For instance, ePub3 requires .xhtml:

It is strongly suggested that you use the .xhtml extension for all EPUB content documents. Browsers will not interpret HTML content as application/xhtml+xml without that extension.

References

查看更多
神经病院院长
7楼-- · 2019-04-07 12:56

IE (including IE8) is the only (significant) browser that doesn't support XHTML served as application/xhtml+xml (corresponds to the xht or xhtml extensions).

Will everyone move to XHTML when IE supports it? Probably not. XML isn't easy to get right, especially if you have to incorporate user-supplied data, such as blog comments. (Though that might be solved with (X)HTML5 <iframe sandbox> before IE starts playing along.) However, I don't have a crystal ball, so we'll just have to wait and see what happens.

查看更多
登录 后发表回答