Activating Browser Modes with Doctype
来源:互联网 发布:python 常用库 编辑:程序博客网 时间:2024/05/18 01:27
In order to deal both with content written according to Web standards and with content written according to legacy practices that were prevalent in the late 1990s, contemporary Web browsers implement various engine modes. This document explains what those mode are and how they are triggered. This document covers mode switching as it applies to Firefox and other Gecko-based browsers, Safari, Chrome and other WebKit-based browsers, Opera, Konqueror, Internet Explorer for Mac and Internet Explorer for Windows and browsers that embed IE. Instead of referring to the names of browser engines, the names of the best-known browsers using each engine are used instead. This document focuses on the mode selection mechanism and does not document the exact behaviors of each mode. Here are the various modes: The choice of the mode for It cannot be stressed enough that the exact behavior of the modes varies from browser to browser even though discussion in this document has been unified. In the Quirks mode the browsers violate contemporary Web format specifications in order to avoid “breaking” pages authored according to practices that were prevalent in the late 1990s. Different browsers implement different quirks. In Internet Explorer 6, 7 and 8, the Quirks mode is effectively frozen IE 5.5. In other browsers, the Quirks mode is a handful of deviations from the Almost Standards mode. If you are authoring new pages now, you are supposed to comply with the relevant specifications (CSS 2.1 in particular) and use the Standards mode. In the Standards mode the browsers try to give conforming documents the specification-wise correct treatment to the extent implemented in a particular browser. Since different browsers are at different stages of compliance, the Standards mode isn’t a single target, either. HTML 5 calls this mode the “no quirks mode”. Firefox, Safari, Chrome, Opera (since 7.5) and IE8 also have a mode known as “the Almost Standards mode”, which implements the vertical sizing of table cells traditionally and not rigorously according to the CSS2 specification. Mac IE 5, Windows IE 6 and 7, Opera prior to 7.5 and Konqueror do not need an Almost Standards mode, because they don’t implement the vertical sizing of table cells rigorously according to the CSS2 specification in their respective Standards modes anyway. In fact, their Standards modes are closer to Mozilla’s Almost Standards mode than to Mozilla’s Standards mode. HTML 5 calls this mode the “limited quirks mode”. IE8 has a mode that is mostly a frozen copy of the mode that was the standards mode in IE7. Other browsers do not have a mode like this, and this mode is not specified by HTML5. In Firefox, Safari, Chrome and Opera, the IE 6, 7 and 8 do not support In the WebKit-based Nokia S60 Browser, the I have not tested Konqueror sufficiently to say what exactly happens in that browser. Some engines have modes that are not relevant to Web content. These modes are only mentioned here for completeness. Opera has a WML 2.0 mode. WebKit on Leopard has a special mode for legacy Dashboard widgets. Here are the main effects of the modes: The modes for In the Almost Standards mode (in browsers that have one), the height of table cells containing only images is computed differently compared to the Standards mode. In the XML mode, selectors have different case-sensitivity behavior. Furthermore, special rules for the HTML There are also some quirks that affect HTML and CSS parsing and would cause conforming pages to be misparsed. These quirks are toggled on and off with quirky layout. However, it is important to realize that the Quirks mode vs. Standards mode is predominantly about CSS layout and parsing—not HTML parsing. Some people misleadingly refer to the Standards mode as “strict parsing mode”, which is misunderstood to imply that browsers enforced HTML syntax rules and that a browser could be used to assess the correctness of markup. This is not the case. The browsers do tag soup fix-ups even when the Standards mode layout is in effect. (In 2000 before Netscape 6 was released, Mozilla actually had parser modes that enforced HTML syntax rules. These modes were incompatible with existing Web content and were abandoned.) Another common misconception is related to XHTML parsing. It is often thought using an XHTML doctype gains different parsing. It does not. XHTML documents served as Only documents served using an XML content type (e.g. Although the Quirks mode is primarily about CSS, there are some scripting quirks as well. For example, in Firefox’s Quirk mode, the HTML In the XML mode, some DOM APIs behave drastically differently, because the DOM API behavior for XML is defined to be incompatible with the HTML behavior. Contemporary browsers use doctype sniffing in order to decide the engine mode for A document type declaration (doctype) is a syntactic artifact of SGML—a legacy markup framework that HTML prior to HTML5 was defined in terms of. In the HTML 4.01 specification, the document type declaration is said to communicate HTML version information. Despite the name “document type declaration” and despite what the HTML 4.01 specification says about “version information”, the document type declaration is not an appropriate means for classifying SGML or XML documents as documents of a particular type even though it seems it was supposed to be (hence the name). (More on this in the addendum.) Neither the HTML 4.01 specification nor ISO 8879 (SGML) says anything about using the document type declaration as an engine mode switch. Doctype sniffing is based on the observation that at the time doctype sniffing was devised the vast majority of quirky documents either didn’t have a document type declaration or they referenced an old DTD. HTML5 acknowledges this reality and defines the doctype in A typical pre-HTML5 document type declaration contains (separated by white space) the string “ Here are simple guidelines for choosing a doctype for a new This is the right thing to do if you want to validate new features such as This doctype also triggers the standards mode, and the decade-old HTML 4.01 validity definition is stable. Please be sure to test your image alignment in Firefox, Safari, Chrome or Opera 9 or 10. Testing image alignment with Internet Explorer is inadequate however be sure to test in IE8, too. This gives you the Almost Standards mode (and the full Standards mode in really old Mozilla releases). Please note that your layouts based on sliced images in tables are likely to break if you later move to HTML5 (and, hence, the full Standards mode). No doctype. Please don’t do this. Willfully designing for the Quirks mode will come and haunt you, your coworkers or your successors in the future—when no one even cares about Windows IE 6 anymore (already no one cares about Netscape 4.x and IE 5). Designing for the Quirks mode is a bad idea. Trust me. If you still want to support Windows IE 6, it is better to apply specific hacks for it using conditional comments than to regress other browsers into the Quirks mode. I am not recommending any of the XHTML doctypes, because serving XHTML as The simple guideline for It was announced on A List Apart that IE8 would use a IE8 has four modes: IE 5.5 quirks mode, IE 7 standards mode, IE 8 almost standards mode and IE 8 standards mode. The choice of mode depends on data from various sources: doctype, a The lucky thing is that IE8 uses doctype sniffing roughly like other browsers if: For the points other than the two In the Unfortunately, without an To counter these effects, a doctype isn’t enough and you need an Here are simple guidelines for choosing an You don’t need to include an Include either the following First, include either the following Then fix your site not to rely on non-standard IE7 behaviors and migrate to Please don’t bring doctype sniffing to XML. Doctype sniffing is a tag soup solution to a tag soup problem. Doctype sniffing was devised after the HTML 4 and CSS2 specs had been written as a heuristic way to distinguish legacy documents from documents whose authors might expect conforming behavior. Sometimes it is suggested that doctype sniffing be used on the XML side as well for dispatching to different handlers, for recognizing the vocabulary in use, or for activating features. This is a bad idea. Dispatching and vocabulary recognition should be based on namespaces and feature activation should be based on explicit processing instructions or elements. The whole concept of well-formedness was introduced to allow DTDless parsing of XML and, by extension, doctypeless documents. In formal terms, if two XML documents have the same canonical form and an app treats them differently (and the difference is not due to opting not to process external entities), chances are the app is broken. In practical terms, if two XML documents cause the same content to be reported (qnames ignored) to SAX2 Moreover, when a lower-level spec defines two things that are equivalent, a higher-level spec should not try to give different meanings to the two things. Consider To put it another way, if you have The reason I don’t bother making the same argument with HTML and SGML is that Web browsers don’t use real SGML parsers for parsing HTML, so I don’t think it is useful to pretend that SGML is being dealt with. However, if you are not convinced yet, please see W. Eliot Kimber’s comp.text.sgml post about the matter. In the following table, Quirks Mode, Standards Mode and Almost Standards Mode are denoted by Q, S and A, respectively. When a browser only has two modes, the Standards Mode is marked as “S”, if the line height in table cells works as in Mozilla’s Standards Mode, and as “A”, if the line height in table cells works as in Mozilla’s Almost Standards Mode. Please note that XHTML served using an XML content type is rendered in the XML mode. The purpose of this table is not to suggest that all the doctypes listed in the table are reasonable choices for new pages. The purpose of this table is to show what data I am basing my recommendations on. The following shorthand notation is used in the column headers: Mozilla’s doctype sniffing code has changed substantially in October 2000, in September 2001 and in June 2002. This document describes the situation with Mozilla builds made available at ftp.mozilla.org on 2000-10-19 and later (and with Netscape 6.x). This document does not address the way doctype sniffing worked in Mozilla M18 (and Netscape 6.0 PR3). Safari’s doctype sniffing code has also changed substantially since the first public beta. This document does not cover the behavior of versions earlier than v73 aka. 0.9. Konqueror’s doctype sniffing code prior to version 3.5 appears to come from a very early version of Safari. Konqueror now matches Safari whose doctype sniffing code comes from Mozilla. As can be seen from the table, Opera’s doctype sniffing has gradually moved from being IE-like to towards being Mozilla-like, although Opera 9.5 and 9.6 regressed on the way. At the same time, the layout behavior of Opera’s Quirks mode has shifted from imitating the Quirks mode of IE 6 towards the Quirks mode of Mozilla. Start: Go to “X-UA-Compatible The steps are available as a flowchart in PDF and PNG formats. Thanks to Simon Pieters, Lachlan Hunt and Anne van Kesteren for their help with correcting the mode table for various Opera versions and for their comments. Thanks to Simon Pieters for the text alternative for the IE8 flowchart.Activating Browser Modes with Doctype
The Scope of this Document
The Modes
Modes for
text/html
Contenttext/html
content depends on doctype sniffing (discussed later in this document). In IE8, the mode also depends on other factors. However, by default even in IE8, the mode depends on the doctype for non-intranet sites that are not on a blacklist supplied by Microsoft.The Mode for
application/xhtml+xml
Content (XML Mode)application/xhtml+xml
HTTP Content-Type
(not a meta
element nor a doctype!) triggers the XML mode. In the XML mode, these browsers give the specification-wise correct treatment for XML documents to the extent implemented in a particular browser.application/xhtml+xml
. Neither does Mac IE 5.application/xhtml+xml
HTTP Content-Type
does not trigger the XML mode due to concerns of compatibility with ill-formed content in mobile walled gardens. (Legacy “mobile browsers” don’t use a real XML parser and, therefore, ill-formed content has been labeled as XML.)Non-Web Modes
The Effects
Layout
text/html
mainly affect CSS layout. For example, not inheriting styles into tables is a quirk. In some browsers, the box model changes to the IE 5.5 box model in the Quirks mode. This document does not emumerate all the layout quirks.body
element do not apply in older versions of browsers that do not implement the latest CSS 2.1 changes.Parsing
text/html
are parsed using the same parser that is used for HTML. As far as browsers are concerned, XHTML served as text/html
is just “tag soup with croutons” (an extra slash here and there).application/xhtml+xml
or application/xml
) trigger the XML mode for parsing in which case the parser is totally different from the HTML parser.Scripting
id
attribute establishes object references from the global scripting scope as in IE. The effects on scripting are more dramatic in IE8 than in other browsers.Doctype Sniffing (aka. Doctype Switching)
text/html
documents. This means that the mode is picked based on the document type declaration (or the lack thereof) at the beginning of an HTML document. (This does not apply to documents served with an XML content type.)text/html
as a mode switch only.<!DOCTYPE
”, the generic identifier of the root element (“html
”), the string “PUBLIC
”, a public identifier of a DTD in quotes, possibly a system identifier (an URL) of the same DTD and the character “>
”. The document type declaration is placed in the document before the start tag of the root element.Choosing a Doctype
text/html
text/html
document:<!DOCTYPE html>
<video>
, <canvas>
and ARIA. Note that the HTML5 validity definition is still in flux. Please be sure to test your image alignment in Firefox, Safari, Chrome or Opera 9 or 10. Testing image alignment with Internet Explorer is inadequate however be sure to test in IE8, too.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
text/html
is considered harmful. If you choose to use an XHTML doctype anyway, please note that the XML declaration makes IE 6 (but not IE 7!) trigger the Quirks mode.application/xhtml+xml
application/xhtml+xml
is not to use a doctype at all. This way the page cannot be “strictly conforming” XHTML 1.0, but that does not matter. (Please see the Addendum below.)IE8 Complications
meta
element-based mode switch in addition to doctype being a factor in the mode choice. (See commentary by Ian Hickson, David Baron, David Baron again, Robert O’Callahan and Maciej Stachowiak.)meta
element, an HTTP header, periodically downloaded data from Microsoft, the intranet zone, settings made by the user, settings made by an intranet administrator, the mode of the frame parent if any and a UI button togglable by the user. (With other apps that embed the engine, the mode also depends on the embedding application.)X-UA-Compatible
HTTP header set by the author.X-UA-Compatible
meta
tag set by the author.X-UA-Compatible
cases, IE8 performs doctype sniffing like IE7. The IE7 emulation is called Compatibility View.X-UA-Compatible
cases, IE8 behaves radically differently from other browsers. Please see an appendix on this page or a flowchart available in PDF and PNG formats.X-UA-Compatible
HTTP header or meta
tag, IE8 lets the user accidentally drop you from the IE8 standards mode to the IE7 mode that emulates the standards mode of IE7 even if you used a proper doctype. Worse, an intranet admin may do this. Also, Microsoft may have blacklisted the entire domain you use (e.g. mit.edu!).X-UA-Compatible
HTTP header or meta
tag.X-UA-Compatible
HTTP header or meta
tag for a new text/html
document that already has a doctype that triggers the standards mode or almost standards mode in other browsers:X-UA-Compatible
HTTP header or meta
tag.meta
element (which in invalid in HTML5) on your page <meta http-equiv="X-UA-Compatible" content="IE=Edge">
(before any script
elements!) or set the following HTTP header on your page: X-UA-Compatible: IE=Edge
meta
element (which in invalid in HTML5) on your page <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
(before any script
elements!) or set the following HTTP header on your page: X-UA-Compatible: IE=EmulateIE7
IE=Edge
.Links to Related Pages
Addendum: A Plea to Implementors and Spec Writers Working with XML
ContentHandler
and an app treats the documents differently, chances are the app is broken. Considering that as a Web author you cannot trust that everyone parsing your pages uses an XML processor that resolves external entities (even if some browsers appear to do so because they map certain public ids to an abridged DTD for entity definitions), inserting a doctype in XML intended for the Web is mostly pointless and often done out of a cargo cultish habit. (You can still validate against a DTD using the DTD override feature of the W3C Validator, although the W3C Validator will say that the result is only tentatively valid. Or better yet, you can use RELAX NG validation, which does not pollute the document with schema references.) Requiring a doctype only for sniffing would be silly, even though that is the situation with HTML in practice.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
. If the public identifier is removed, the same DTD is still designated and, therefore, the doctype <!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
means the same as the previous doctype. Should they be sniffed differently? The idea can be carried further. Suppose the DTD is copied to example.com and named foobar.dtd: <!DOCTYPE html SYSTEM "http://example.com/foobar.dtd">
. How could that one be sniffed? The meaning is the same. The whole DTD could even be pasted inline!#include "foo.h"
, you should not bind any black magic to the name foo.h
, because it should be permissible to paste the contents of foo.h
inline or copy the contents of foo.h
to bar.h
and say #include "bar.h"
.Appendix: Handling of Some Doctypes in
text/html
X-UA-Compatible
override nor a Compatibility View override (“A” means the IE8 Almost Standards mode in this case), Opera 7.5…8.54 and 9.5…9.6X-UA-Compatible
override (in this case “A” means the IE7 mode) and Opera 7.10…7.23Doctype NS6 Old Moz Moz &
Safari &
Opera 10
& HTML5Opera 9.0 IE 8 & Opera 9.5 IE 7 & Opera 7.10 IE 6 & Opera 7.0 Mac IE 5 Konq 3.2 NoneQQQQQQQQQ<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
QQQQQQQQQ<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
SSSSSAAAA<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
SSSSSAAQA<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/html4/strict.dtd">
SSSSSAAAA<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
SSSSSAAAA<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
QQQQQQQQQ<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
QQQQQQQQQ<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
SSAAAAAAQ<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
QSAAAAAAQ<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
QQQQAAAAQ<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
SSSSSAAAA<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
SSSSSAAAA<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
SSSSSAAAA<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
SSAAAAAAQ<?xml version="1.0" encoding="UTF-8"?>
SSSSSAQAQ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><?xml version="1.0" encoding="UTF-8"?>
SSSSSAQAQ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd"><?xml version="1.0" encoding="UTF-8"?>
SSSSSAQAQ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><?xml version="1.0" encoding="UTF-8"?>
SSAAAAQAQ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!DOCTYPE HTML PUBLIC "ISO/IEC 15445:2000//DTD HTML//EN">
QSSQQQQQQ<!DOCTYPE HTML PUBLIC "ISO/IEC 15445:2000//DTD HyperText Markup Language//EN">
QSSSSAAAQ<!DOCTYPE HTML PUBLIC "ISO/IEC 15445:1999//DTD HTML//EN">
SSSQQQQQQ<!DOCTYPE HTML PUBLIC "ISO/IEC 15445:1999//DTD HyperText Markup Language//EN">
SSSSSAAAQ<!DOCTYPE html>
QSSSSAAAHistorical Notes
Appendix: IE8’s Mode Selection
meta
?”.meta
?script
first: Go to “X-UA-Compatible HTTP Header?”.Acknowledgments
- Activating Browser Modes with Doctype
- 用Doctype激活浏览器模式(Activating Browser Modes with Doctype) (一)
- 用Doctype激活浏览器模式(Activating Browser Modes with Doctype) (二)
- 用Doctype激活浏览器模式(Activating Browser Modes with Doctype) (三)
- 用Doctype激活浏览器模式(Activating Browser Modes with Doctype) (四)
- 用Doctype激活浏览器模式(Activating Browser Modes with Doctype) (五)
- Headless Browser Testing With Xvfb
- Introduction to Programming with c++ 13-5 File Open Modes
- Intra Mode Coding With 67 Intra Prediction Modes
- Fix Your Site With the Right DOCTYPE!
- Fix Your Site With the Right DOCTYPE!
- Fix Your Site With the Right DOCTYPE!
- DOCTYPE
- doctype
- DOCTYPE
- DOCTYPE
- doctype
- DOCTYPE
- 中航协否认延误航班补偿新规定
- 文件下载方法 asp.net
- C++如何取得当前程序(控制台应用程序)自己的文件名
- 大家好
- 用.net实现简单的邮件发送(异步方式)
- Activating Browser Modes with Doctype
- VC辅助开发工具大全
- Java线程相关
- “quirks mode”&“strict mode”的一些整理
- java中获取程序路径方法整理
- bash的几个初始化文件
- Matlab 自定义函数
- SecureCRT上传项目、解压、部署
- 淡定