FAQ Topic - How do I detect Opera/Safari/IE? (2012-02-19)

Discussion in 'Javascript' started by FAQ server, Feb 19, 2012.

  1. FAQ server

    FAQ server Guest

    -----------------------------------------------------------------------
    FAQ Topic - How do I detect Opera/Safari/IE?
    -----------------------------------------------------------------------

    The short answer: _Don't do that_.

    The `navigator` host object contains properties which
    may identify the browser and version. These properties are historically
    inaccurate. Some browsers allow the user to set `navigator.userAgent` to any value. For
    example, Firefox, (type `about:config` and search `useragent`
    or Safari, `Develop > User Agent > Other...`, IE, via Registry.

    Other browsers, such as Opera, provide a list of user agents
    for the user to select from. There are also at least 25 other
    javascript capable browsers, with multiple versions, each
    with their own string.

    Browser detection is unreliable, at best. It usually causes
    forward-compatibility and maintenance problems. It is unrelated to the
    problem or incompatiblity it is trying to solve and obscures the
    problems it is used for, where it is used.

    Object detection is checking that the object in question exists.

    http://dev.opera.com/articles/view/using-capability-detection/

    goes one step further to actually test the object,
    method, or property, to see if behaves in the desired manner.

    Feature Test Example:

    /**
    * Returns the element/object the user targeted.
    * If neither DOM nor IE event model is supported, returns undefined.
    * @throws TypeError if the event is not an object.
    */
    function getEventTarget(e) {
    e = e || window.event;
    // First check for the existence of standard "target" property.
    return e.target || e.srcElement;
    }

    <URL: http://jibbering.com/faq/notes/detect-browser/>
    <URL: http://dev.opera.com/articles/view/using-capability-detection/>
    <URL: http://developer.apple.com/internet/webcontent/objectdetection.html>
    <URL: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43>


    The complete comp.lang.javascript FAQ is at
    http://jibbering.com/faq/

    --

    The sendings of these daily posts are proficiently hosted
    by http://www.pair.com.
     
    FAQ server, Feb 19, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hkaiser
    Replies:
    3
    Views:
    598
    Ebenezer
    Jan 13, 2012
  2. FAQ server
    Replies:
    19
    Views:
    237
    Richard Cornford
    Sep 10, 2006
  3. FAQ server
    Replies:
    1
    Views:
    243
    Dr J R Stockton
    Oct 16, 2008
  4. Garrett Smith

    FAQ Question: How do I detect Opera/Netscape/IE

    Garrett Smith, Jun 1, 2009, in forum: Javascript
    Replies:
    59
    Views:
    454
    Garrett Smith
    Jun 9, 2009
  5. FAQ server
    Replies:
    0
    Views:
    1,207
    FAQ server
    Feb 22, 2012
Loading...

Share This Page