How to determine the browser type on the client (detecting IE7 + in Javascript)

Source: Internet
Author: User

Today I was updating some JavaScript code to support the rapidly-approaching Internet Explorer 7. there were a few places in the Code where there were ie-specific workarounds, which happily are no longer needed in IE 7
Thanks to its improved standards support. Yay position: fixed!

Where the code used to check for if (IE ){...}, now I wanted it to check for if (ie6orlower ){...}. so how to you tell the difference between IE 6 and IE 7 +? You cocould parse the User-Agent string, but I 'd rather detect changes in the JavaScript Object Model. Here's what I came up:

If (typeof document. Body. style. maxheight! = "Undefined "){
// IE 7, Mozilla, Safari, opera 9
} Else {
// IE6, older browsers
}
This distinguishes between browsers based on the fact that IE 7 knows about the maxheight CSS property, whereas previous versions of IE didn't. Does that seem like a sane approach to you?

Update: over at ajaxian Arjan points out that it's a bit simpler to check for window. XMLHttpRequest, which is also new in IE 7.

Javascript:

  1.  
  2. If (TypeofDocument.Body.Style.Maxheight! ="Undefined") {
  3. // IE 7, Mozilla, Safari, opera 9
  4. } Else {
  5. // IE6, older browsers
  6. }


    You can also use the xhr check:


  7. If (window. XMLHttpRequest) {
    // IE 7, Mozilla, Safari, opera 9
    }else {
    // IE6, older browsers
    }

    if script is executed width, then document. body is not available yet.

    If (window. XMLHttpRequest) {
    If (document. epando) {
    alert ("IE7");
    // IE7
    }else {
    // Mozilla, Safari, opera 9... ETC
    alert ("Mozilla");
    }< BR >}else {
    // IE6, older browsers
    alert ("IE6 ");
    }< br>

    I tested it and found that the last method is different from IE7 or Firefox. Please kindly advise!

Related Article

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.