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.


    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
    FAQ server, Feb 19, 2012
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.