Simple browser detect

Discussion in 'Javascript' started by Szar, Nov 13, 2003.

  1. Szar

    Szar Guest

    JS noob. I've seen plenty of browser detection scripts but they all seem to
    be slightly different and don't really fit my needs. I have various places
    where if the browser is IE I'd like to display [this html code] else
    [display this]. For example, if a browser is IE I want to use this CSS file
    otherwise use a different one and if it's IE make this cell x pixels high
    else make it y pixels high. I'm sure this is easy, please excuse my
    stupidity.
    Steve.
    Szar, Nov 13, 2003
    #1
    1. Advertising

  2. Szar

    Jerry Park Guest

    Szar wrote:
    > JS noob. I've seen plenty of browser detection scripts but they all seem to
    > be slightly different and don't really fit my needs. I have various places
    > where if the browser is IE I'd like to display [this html code] else
    > [display this]. For example, if a browser is IE I want to use this CSS file
    > otherwise use a different one and if it's IE make this cell x pixels high
    > else make it y pixels high. I'm sure this is easy, please excuse my
    > stupidity.
    > Steve.
    >
    >

    Browser detection scripts are generally a bad idea. There are multiple
    browsers and multiple versions of those browsers running. Many browsers
    lie, and many can be set to say they are anything their user wants them
    to say. Your detection script, no matter how good, cannot reliably
    determine what the browser is.

    Almost without exception, when a site fails to operate properly in my
    browser, I find that it is because of a browser detection script and
    javascript which fails to serve the page because either the site
    designer doesn't want to support my browser and deliberately excludes
    it, or because the site designer thinks erroneously that my browser
    can't render the page, or because the site designer has erroneously
    identified my browser as some other browser or browser version.

    It is far better to write to standards. When you write to standards and
    a page doesn't display properly, the client knows that it is the fault
    of his browser -- not because of your code.
    Jerry Park, Nov 13, 2003
    #2
    1. Advertising

  3. Jerry Park <> writes:

    > Szar wrote:
    >> JS noob. I've seen plenty of browser detection scripts but they all seem to
    >> be slightly different and don't really fit my needs. I have various places
    >> where if the browser is IE I'd like to display [this html code] else
    >> [display this]. For example, if a browser is IE I want to use this CSS file
    >> otherwise use a different one and if it's IE make this cell x pixels high
    >> else make it y pixels high. I'm sure this is easy, please excuse my
    >> stupidity.
    >> Steve.
    >>

    > Browser detection scripts are generally a bad idea. There are multiple
    > browsers and multiple versions of those browsers running. Many
    > browsers lie, and many can be set to say they are anything their user
    > wants them to say. Your detection script, no matter how good, cannot
    > reliably determine what the browser is.
    >
    > Almost without exception, when a site fails to operate properly in my
    > browser, I find that it is because of a browser detection script and
    > javascript which fails to serve the page because either the site
    > designer doesn't want to support my browser and deliberately excludes
    > it, or because the site designer thinks erroneously that my browser
    > can't render the page, or because the site designer has erroneously
    > identified my browser as some other browser or browser version.
    >
    > It is far better to write to standards. When you write to standards
    > and a page doesn't display properly, the client knows that it is the
    > fault of his browser -- not because of your code.


    You overestimate the common user of IE, the type that think the
    "e"-icon opens "the internet". He never heard of other browsers, an
    all that matters is that the page works for him.

    Otherwise I agree: Write to standards, but allow for a few fall-backs
    if the browser doesn't support standards well enough (like IE and CSS2).

    The best way to make a fallback, is to use object detection, not
    browser detection. That is, instead of:
    ---
    if (browseDetect() == "IE") {
    var elem = document.all['foo'];
    } else {
    var elem = document.getElementById("foo");
    }
    ---
    you do:
    ---
    if (document.getElementById) {
    var elem = document.getElementById("foo");
    } else if (document.all) {
    var elem = document.all['foo'];
    } else {
    // handle error
    }
    ---
    Points to notice: Default is the standard method, getElementById. Even
    if some new browsers understands document.all, they will use the standard
    method first. It is safer that way.
    There are browsers other than IE that support document.all, and browsers
    that claim to be IE that doesn't support it. Testing for the object you
    need directly avoids pitfalls like these.
    Be ready to handle the complete failur, just as you are prepared for
    Javascript to be completely unavailable.


    The safest way to make a specific fallback for IE is to use the
    IE-proprietary conditional comments:
    <!--[if IE]>
    .... all this is only seen by IE, all other browsers think it is
    a normal HTML comment
    <![end if]-->
    However, these only work in IE 5+, and maybe the bug will be fixed in
    IE 7, so you should include the version of IE:
    <!--[if IE 6]> ... only IE 6 and lower ...

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Nov 13, 2003
    #3
  4. Szar

    Jerry Park Guest

    Lasse Reichstein Nielsen wrote:
    > Jerry Park <> writes:
    >
    >
    >>Szar wrote:
    >>
    >>>JS noob. I've seen plenty of browser detection scripts but they all seem to
    >>>be slightly different and don't really fit my needs. I have various places
    >>>where if the browser is IE I'd like to display [this html code] else
    >>>[display this]. For example, if a browser is IE I want to use this CSS file
    >>>otherwise use a different one and if it's IE make this cell x pixels high
    >>>else make it y pixels high. I'm sure this is easy, please excuse my
    >>>stupidity.
    >>>Steve.
    >>>

    >>
    >>Browser detection scripts are generally a bad idea. There are multiple
    >>browsers and multiple versions of those browsers running. Many
    >>browsers lie, and many can be set to say they are anything their user
    >>wants them to say. Your detection script, no matter how good, cannot
    >>reliably determine what the browser is.
    >>
    >>Almost without exception, when a site fails to operate properly in my
    >>browser, I find that it is because of a browser detection script and
    >>javascript which fails to serve the page because either the site
    >>designer doesn't want to support my browser and deliberately excludes
    >>it, or because the site designer thinks erroneously that my browser
    >>can't render the page, or because the site designer has erroneously
    >>identified my browser as some other browser or browser version.
    >>
    >>It is far better to write to standards. When you write to standards
    >>and a page doesn't display properly, the client knows that it is the
    >>fault of his browser -- not because of your code.

    >
    >
    > You overestimate the common user of IE, the type that think the
    > "e"-icon opens "the internet". He never heard of other browsers, an
    > all that matters is that the page works for him.
    >
    > Otherwise I agree: Write to standards, but allow for a few fall-backs
    > if the browser doesn't support standards well enough (like IE and CSS2).
    >
    > The best way to make a fallback, is to use object detection, not
    > browser detection. That is, instead of:
    > ---
    > if (browseDetect() == "IE") {
    > var elem = document.all['foo'];
    > } else {
    > var elem = document.getElementById("foo");
    > }
    > ---
    > you do:
    > ---
    > if (document.getElementById) {
    > var elem = document.getElementById("foo");
    > } else if (document.all) {
    > var elem = document.all['foo'];
    > } else {
    > // handle error
    > }
    > ---
    > Points to notice: Default is the standard method, getElementById. Even
    > if some new browsers understands document.all, they will use the standard
    > method first. It is safer that way.
    > There are browsers other than IE that support document.all, and browsers
    > that claim to be IE that doesn't support it. Testing for the object you
    > need directly avoids pitfalls like these.
    > Be ready to handle the complete failur, just as you are prepared for
    > Javascript to be completely unavailable.
    >
    >
    > The safest way to make a specific fallback for IE is to use the
    > IE-proprietary conditional comments:
    > <!--[if IE]>
    > .... all this is only seen by IE, all other browsers think it is
    > a normal HTML comment
    > <![end if]-->
    > However, these only work in IE 5+, and maybe the bug will be fixed in
    > IE 7, so you should include the version of IE:
    > <!--[if IE 6]> ... only IE 6 and lower ...
    >
    > /L

    I agree. My website is XHTML1.0 compliant. I had to write some css
    specifically to make IE center a division properly. Fortunately, you can
    sometimes correct browser errors without messing up the compliant
    browsers. However, if I must choose between standards compliant code or
    non-compliant code (just to allow a non-compliant browser to display the
    page), I'll choose compliant code, for no other reason than that people
    should be encouraged to write to the standards.

    The code many people write, where there are 47 versions of a page for
    multiple browsers, is a nightmare. I can't imagine why anyone would want
    to do that.
    Jerry Park, Nov 13, 2003
    #4
  5. Szar

    cwdjr Guest

    Please see the page at
    http://www.wtv-zone.com/cwdjrsxyz/browser_sniffer_tests.html .Here I
    have given fairly detailed browser sniffing results for 6 browsers. At
    the bottom of the page are a few comments that may
    help.__________________________________________
    60000 Year Perpetual Calendar. Official US Holidays are detected and
    colored. Try to put in bad data. Validation sinks to a new low. The
    calendar is at http://www.wtv-zone.com/cwdjrsxyz/calendar/perpetual_calendar3.html
    ..Some of the details are given at the page
    http://www.wtv-zone.com/cwdjrsxyz/calendar/perpCal3_ReadMe.html
    .._________________________
    cwdjr, Nov 14, 2003
    #5
  6. Szar wrote:

    > JS noob. I've seen plenty of browser detection scripts but they all seem to
    > be slightly different and don't really fit my needs.


    You do not need and you do not want such "detection" scripts:

    http://pointedears.de.vu/scripts/test/whatami


    PointedEars
    Thomas 'PointedEars' Lahn, Nov 14, 2003
    #6
  7. Szar

    Szar Guest

    "Szar" <> wrote in message
    news:KGFsb.260$...
    > JS noob. I've seen plenty of browser detection scripts but they all seem

    to
    > be slightly different and don't really fit my needs. I have various places
    > where if the browser is IE I'd like to display [this html code] else
    > [display this]. For example, if a browser is IE I want to use this CSS

    file
    > otherwise use a different one and if it's IE make this cell x pixels high
    > else make it y pixels high. I'm sure this is easy, please excuse my
    > stupidity.
    > Steve.
    >
    >


    In the time it took everyone to write their opinions on why i should not use
    brwoser detection couldn't they have just answered my question??? This is
    for a page that only myself and one other person will be accessing and I
    frankly don't care if it doesn't look good on 100% off all browsers out
    there. I care about current IE and Mozilla versions, that's all. The
    difference with how IE and others view page margins and a few other things
    are throwing off how i'd like images on the page to line up with the
    background image. If you want to answer a post then maybe give a little side
    note, fine. Otherwise save it.
    Szar, Nov 14, 2003
    #7
  8. Szar wrote:

    > In the time it took everyone to write their opinions on why i should not use
    > brwoser detection couldn't they have just answered my question???


    Your `?' key is b0rken.

    They/I could have just answered your question, but this is a discussion
    group, no a (paid) support forum. All you get is advice, BTW for free.


    PointedEars
    Thomas 'PointedEars' Lahn, Nov 14, 2003
    #8
    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. sm mehta

    how to detect Browser close event.

    sm mehta, Jul 29, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    5,154
    Steve C. Orr, MCSD
    Jul 29, 2003
  2. hb
    Replies:
    6
    Views:
    729
  3. Lord2702
    Replies:
    3
    Views:
    598
  4. Lord2702

    How to detect browser close

    Lord2702, Oct 30, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    15,517
    Ken Dopierala Jr.
    Oct 31, 2004
  5. Replies:
    0
    Views:
    468
Loading...

Share This Page