SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    some code of making a condition to the IE browser?

    HTML Code:
     if (navigator.appName == "Microsoft Internet Explorer") {
    begin = navigator.userAgent.indexOf("MSIE ") +
    "MSIE ".length;
    if(navigator.userAgent.indexOf(";", begin) > 0) {
    end = navigator.userAgent.indexOf(";", begin);
    } else {
    end = navigator.userAgent.indexOf(")", begin)
    + 2;
    }
    document.getElementById("targetDiv").innerHTML =
    "You are using Internet Explorer " +
    navigator.userAgent.substring(begin, end);
    }
    }
    i don't know why there added an else part? what's the meaning and effect of it? thank you,

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runeveryday View Post
    i don't know why there added an else part? what's the meaning and effect of it? thank you,
    It's to allow for a couple of different variations in how the userAgent text is formatted.

    Please do not use such browser sniffing techniques though. They are incredibly brittle because browsers and their capabilities change over time, so instead use object detection instead.

    For example:

    Code javascript:
    if ( !Array.prototype.forEach ) {
       ...
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •