JavaScript document. compatmode compatibility Application Method

Source: Internet
Author: User

The rendering of the box model by IE is very different in standards mode and quirks mode. The interpretation of the Box Model in standards mode is the same as that in other standard browsers, however, in the quirks mode, there is a big difference. In the case that doctype is not declared, ie is the quirks mode by default. Therefore, for compatibility consideration, we may need to obtain the current document rendering method.

Document. compatmode comes in handy. It has two possible return values: backcompat and css1compat.

Backcompat: Disabled in standard compatibility mode. The client area width of the browser is document. Body. clientwidth;
Css1compat: enabled in standard compatibility mode. The client width of the browser is document.doc umentelement. clientwidth.

Therefore, you have written an example to accurately obtain the width and height of the client area of the webpage, the width and height of the scroll bar, and the left and top pages of the scroll bar.Code:

If (document. compatmode = "backcompat "){
Cwidth = Document. Body. clientwidth;
Cheight = Document. Body. clientheight;
Swidth = Document. Body. scrollwidth;
Sheight = Document. Body. scrollheight;
Sleft = Document. Body. scrollleft;
Stop = Document. Body. scrolltop;
}
Else {// document. compatmode = "css1compat"
Cwidth = document.doc umentelement. clientwidth;
Cheight = document.doc umentelement. clientheight;
Swidth = document.doc umentelement. scrollwidth;
Sheight = document.doc umentelement. scrollheight;
Sleft = document.doc umentelement. scrollleft = 0? Document. Body. scrollleft: document.doc umentelement. scrollleft;
Stop = document.doc umentelement. scrolltop = 0? Document. Body. scrolltop: document.doc umentelement. scrolltop;
}

 

// Get page height
Function pageheight (){
If ($. browser. MSIE ){
Return document. compatmode = "css1compat "? Document.doc umentelement. clientheight:
Document. Body. clientheight;
} Else {
Return self. innerheight;
}
};

// Get page width
Function pagewidth (){
If ($. browser. MSIE ){
Return document. compatmode = "css1compat "? Document.doc umentelement. clientwidth:
Document. Body. clientwidth;
} Else {
Return self. innerwidth;
}
};

 

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.