Two browser modes: quirks mode and strict mode

Source: Internet
Author: User

When looking at js code, I sometimes see things about quirks mode (weird mode) and strict mode (strict format). I haven't gotten into details yet, but it's just a bit of a broken concept, I can't take this kind of feeling recently. I decided to take a look at it.

1. quirks mode and strict mode are two modes for the browser to parse css, or they can be called resolution methods. Currently, the browser is supported in both modes.

2. historical reasons. When the early browsers Netscape 4 and Explorer 4 parse css but do not comply with W3C standards, the parsing method is called quirks mode (weird mode ), however, as W3C standards become more and more important, many browsers begin to parse CSS according to W3C standards. We use strict mode to parse CSS following W3C standard ).

3. Although later browsers support strict mode, many browsers did not give up supporting quirks mode.

An important reason is that a large number of webpages developed in quirks mode can be correctly displayed. Therefore, when these browsers support two modes, when obtaining a webpage, a preliminary task is to determine the method used for parsing (the judgment condition will be described later ). Note: Explorer 5 still only supports quirks mode
4. How does the browser determine which method to parse CSS?

The solution is to use the doctype statement. Most browsers use the following judgment rules:

  • The browser needs to make the old web page work normally, but this part of the web page does not have the doctype Declaration, so the browser uses the quirks mode resolution for the web page without the doctype declaration.
  • For web pages with doctype declaration, what browser uses what mode resolution, here is a detailed list can refer to: http://hsivonen.iki.fi/doctype/
  • For webpages with doctype declarations, there are several simple rules for judgment:

For doctype declarations that cannot be recognized by browsers, the browser uses strict mode for parsing in the doctype declaration. If the DTD declaration is not used or the DTD declaration below HTML4 (excluding HTML4) is used, basically all browsers use quirks mode for rendering, while others use strict mode for parsing. In this case, the vast majority of existing webpages with doctype declarations use strict mode for parsing. In ie6, if there is an xml declaration before the doctype declaration (for example: <? Xml version = "1.0" encoding = "iso-8859-1"?> ), The quirks mode is used for parsing. This rule has been removed from ie7.

5. mozilla proposed almost strict moede, which is different from strict mode:

The img element is inline in strict and block in almost strict mode. As we mentioned above, in most browsers, most doctype declarations trigger almost strict mode;

6. The biggest difference between the quirks mode and the strict mode is to mention the explanation of the box mode, which is also worth attention in js.

What is the box mode? This is for block-level elements. I will just give you a brief description here (For details, refer to relevant materials ). To put it bluntly, we think of block-level elements as a box for loading things. The css attributes such as margin, padding, border, and width constitute the box mode. The difference is that it is generated on the width attribute:

In strict mode:

Width indicates the content width, that is, true width of the element = margin-left + border-left-width + padding-left + width + padding-right + border-right-width + margin-right;

 

In quirks mode:

Width indicates the actual width of the element, content width = width-(margin-left + margin-right + padding-left + padding-right + border-left-width + border-right-width)

Here we will record the items related to the box mode!

· Inline elements, such as <a> and <span>, define the upper and lower boundary and do not affect the line-height. The distance between inline elements and the element of the previous row is determined by the Row height, instead of filling or boundary. Note 2: The inline element (display: inline) does not need to be displayed in the new line, and does not force the line break of the subsequent element. For example, a, em, and span are all inline elements. An inline element can be a child element of any other element.

· The floating element (whether Left or Right floating) boundary is not compressed. If the floating element does not declare the width, its width tends to 0, that is, the minimum width that can be compressed to its content.

· If there is no content in the box, even if both the width and height are defined as 100%, it actually only accounts for 0%, so it will not be displayed. Pay special attention to this point when adopting layer layout.

· The boundary value can be negative, and the display effect may vary with browsers.

· The fill value cannot be negative.

· The default border style (border-style) is not displayed (none ).

7. There is another difference between quirks mode and strict mode, but it has not been verified. I just found this phenomenon.

In strict mode,

The css attribute font-size of a table inherits the font size of the parent element, that is, the font size of the table inherits the font size of the parent element.

In quirks mode,

The font-size attribute of table css does not inherit the parent element. You need to set it specially. That is to say, the font size in the table does not inherit the font size of the parent element.

8. How can I determine in js how the browser is parsed?
The document object has a property compatMode, which has two values:
BackCompat corresponds to quirks mode
CSS1Compat corresponds to strict mode

9. Determine whether the browser is compatible with common code:

Var ua = navigator. userAgent. toLowerCase ();
Var client = {
IsStrict: document. compatMode = 'css1compat ',
IsOpera: ua. indexOf ('Opera ')>-1,
IsIE: ua. indexOf ('msie ')>-1,
IsIE7: ua. indexOf ('msie 7')>-1,
IsSafari:/webkit | khtml/. test (ua ),
IsWindows: ua. indexOf ('windows ')! =-1 | ua. indexOf ('win32 ')! =-1,
IsMac: ua. indexOf ('macintosh ')! =-1 | ua. indexOf ('mac OS x ')! =-1,
IsLinux: ua. indexOf ('linux ')! =-1
};
Client. isBorderBox = client. isIE &&! Client. isStrict;
Client. isSafari3 = client. isSafari &&!! (Document. evaluate );
Client. isGecko = ua. indexOf ('gecko ')! =-1 &&! Client. isSafari;

/**
* You're not sill using IE6 are you?
*/
Var ltIE7 = client. isIE &&! Client. isIE7;

 

10. Simple code to block the right mouse button in FF and IE:

 

Document. oncontextmenu = function (e ){
// Debugger; // used for debugging
Return! (E | event). cancelBubble = true );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.