Browser version detection: How?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R. Smits

    #1

    Browser version detection: How?

    I've have got this script, the only thing I want to be changed is the first
    part. It has to detect IE version 6 instead of just "Microsoft Internet
    Explorer". Can somebody help me out?
    I tried "Microsoft Internet Explorer 6" but that doesn't work.

    <SCRIPT LANGUAGE="Javas cript">
    <!--
    bName = navigator.appNa me;
    if (bName =="Microsoft Internet Explorer") {
    document.write( '<link media="screen" rel="STYLESHEET " type="text/css"
    href="styleshee t1.css">')
    } else {
    document.write( '<link media="screen" rel="STYLESHEET " type="text/css"
    href="styleshee t2.css">')
    }
    //-->
    </SCRIPT>


  • Michael Winter

    #2
    Re: Browser version detection: How?

    On Mon, 18 Oct 2004 09:14:11 +0200, R. Smits <NOMAIL@NOMAI L> wrote:
    [color=blue]
    > I've have got this script, the only thing I want to be changed is the
    > first part. It has to detect IE version 6 instead of just "Microsoft
    > Internet Explorer". Can somebody help me out?
    > I tried "Microsoft Internet Explorer 6" but that doesn't work.[/color]

    Scripting browser detection is highly unreliable, as browsers can, and do,
    spoof IE. It would be more reliable to use conditional comments[1].

    <link rel="stylesheet " type="text/css" href="styleshee t2.css"
    media="screen">

    <!--[if IE 6]>
    <link rel="stylesheet " type="text/css" href="styleshee t1.css"
    media="screen">
    <[endif]-->

    This would load stylesheet2 in all browsers, and stylesheet1 in only IE6.
    The rules in stylesheet1 would override conflicting rules in stylesheet2.
    [color=blue]
    > <SCRIPT LANGUAGE="Javas cript">[/color]

    The language attribute has been deprecated in favour of the (required)
    type attribute for quite some time now. Don't use it.

    <script type="text/javascript">
    [color=blue]
    > <!--[/color]

    The practice of hiding scripts is similarly out-dated. There are no user
    agents now in use that will render the contents of the script. All
    browsers understand what a SCRIPT element is, even if they will just
    ignore it (they don't support scripting).
    [color=blue]
    > bName = navigator.appNa me;[/color]

    You might want to read the FAQ (<URL:http://jibbering.com/faq/>). Browser
    detection, and what you should do instead, is covered there.

    [snip]

    Hope that helps,
    Mike


    [1]
    <URL:http://msdn.microsoft. com/workshop/author/dhtml/overview/ccomment_ovw.as p>

    Only IE 5 and later support conditional comments.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Thomas Hoheneder

      #3
      Re: Browser version detection: How?

      > I've have got this script, the only thing I want to be changed is the
      first[color=blue]
      > part. It has to detect IE version 6 instead of just "Microsoft Internet
      > Explorer". Can somebody help me out?[/color]

      The question is - do you really need exactly IE 6.x or would it be enough to
      get some IE 4.x or higher version?
      Consider, that if only asking for IE 6.x an IE 5.x e. g. would get
      stylesheet2.css .
      So I think you should use one stylesheet file for all IE 4.x or higher
      version and some other stylesheet for all none-IE browsers (assuming that
      there is no IE older than 4.x in use today).
      Or do you really have such special stylesheet features that are only
      supported by IE 6.x?

      Nice greetings from
      Thomas


      Comment

      • R. Smits

        #4
        Re: Browser version detection: How?

        "Thomas Hoheneder" <thomas.hohened er@gmx.de> wrote in message
        news:cl08oi$q1q $1@domitilla.ai oe.org...[color=blue]
        > The question is - do you really need exactly IE 6.x or would it be enough[/color]
        to[color=blue]
        > get some IE 4.x or higher version?
        > Consider, that if only asking for IE 6.x an IE 5.x e. g. would get
        > stylesheet2.css .
        > So I think you should use one stylesheet file for all IE 4.x or higher
        > version and some other stylesheet for all none-IE browsers (assuming that
        > there is no IE older than 4.x in use today).
        > Or do you really have such special stylesheet features that are only
        > supported by IE 6.x?[/color]

        Yep, I defined a 'overflow-y'.
        As far as I know IE 6 is the only browers which supports this.
        (Am I correct?)


        Comment

        • R. Smits

          #5
          Re: Browser version detection: How?

          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opsf16aoe1 x13kvk@atlantis ...[color=blue]
          > On Mon, 18 Oct 2004 09:14:11 +0200, R. Smits <NOMAIL@NOMAI L> wrote:
          >[color=green]
          > > I've have got this script, the only thing I want to be changed is the
          > > first part. It has to detect IE version 6 instead of just "Microsoft
          > > Internet Explorer". Can somebody help me out?
          > > I tried "Microsoft Internet Explorer 6" but that doesn't work.[/color]
          >
          > Scripting browser detection is highly unreliable, as browsers can, and do,
          > spoof IE. It would be more reliable to use conditional comments[1].
          >
          > <link rel="stylesheet " type="text/css" href="styleshee t2.css"
          > media="screen">
          >
          > <!--[if IE 6]>
          > <link rel="stylesheet " type="text/css" href="styleshee t1.css"
          > media="screen">
          > <[endif]-->
          >
          > This would load stylesheet2 in all browsers, and stylesheet1 in only IE6.
          > The rules in stylesheet1 would override conflicting rules in stylesheet2.[/color]

          I tried something like this inside one stylesheet, but it didn't work.
          Something like this:

          ..scroll {
          height: 450px;
          display:block;
          overflow: scroll;
          overflow-y: scroll;
          }
          [color=blue][color=green]
          > > <SCRIPT LANGUAGE="Javas cript">[/color]
          >
          > The language attribute has been deprecated in favour of the (required)
          > type attribute for quite some time now. Don't use it.
          >
          > <script type="text/javascript">
          >[color=green]
          > > <!--[/color]
          >
          > The practice of hiding scripts is similarly out-dated. There are no user
          > agents now in use that will render the contents of the script. All
          > browsers understand what a SCRIPT element is, even if they will just
          > ignore it (they don't support scripting).
          >[color=green]
          > > bName = navigator.appNa me;[/color]
          >
          > You might want to read the FAQ (<URL:http://jibbering.com/faq/>). Browser
          > detection, and what you should do instead, is covered there.
          >
          > [snip]
          >
          > Hope that helps,
          > Mike
          >
          >
          > [1]
          >[/color]
          <URL:http://msdn.microsoft.com/workshop/a...ccomment_ovw.a
          sp>[color=blue]
          >
          > Only IE 5 and later support conditional comments.[/color]

          Thanks for your help.



          Comment

          • Thomas Hoheneder

            #6
            Re: Browser version detection: How?

            > Yep, I defined a 'overflow-y'.[color=blue]
            > As far as I know IE 6 is the only browers which supports this.
            > (Am I correct?)[/color]

            I am not sure, but I think you might be right. I tried it on IE 6 and it
            worked, and on Mozilla Firefox it did not work.
            So I think you want to have a query to detect IE 6.x on one side and all
            other browsers on the other side. For the moment (where is no IE 7.x) I
            would do this in the following way:

            var strBVersion = new String(navigato r.appVersion);
            var bVersion = str.indexOf("MS IE 6");
            if (bName == "Microsoft Internet Explorer" && bVersion != -1)
            // IE 6.x
            else
            // some IE older than IE 6 or none IE browsers

            But it could be (which is very presumably) an IE 7.x or something like that.
            To support also this one, you should better modify the above instructions in
            the following way to support all IE 6.x or higher versions - so a more
            robust query would be:

            var strBVersion = new String(navigato r.appVersion);
            var bVersion1 = str.indexOf("MS IE 6");
            var bVersion2 = str.indexOf("MS IE 7");
            if (bName == "Microsoft Internet Explorer" && bVersion1 != -1 && (bVersion1
            != -1 || bVersion2 != -1))
            // IE 6.x or higher
            else
            // some IE older than IE 6 or none IE browser

            Hope this helps you.

            Nice greetings from
            Thomas


            Comment

            • Thomas Hoheneder

              #7
              Re: Browser version detection: How?

              > var bVersion = str.indexOf("MS IE 6");

              I'm sorry, but "str.indexO f" must be replaced with "strBVersion.in dexOf" in
              both code variations.


              Comment

              • Randy Webb

                #8
                Re: Browser version detection: How?

                Thomas Hoheneder wrote:
                [color=blue][color=green]
                >>Yep, I defined a 'overflow-y'.
                >>As far as I know IE 6 is the only browers which supports this.
                >>(Am I correct?)[/color]
                >
                >
                > I am not sure, but I think you might be right. I tried it on IE 6 and it
                > worked, and on Mozilla Firefox it did not work.
                > So I think you want to have a query to detect IE 6.x on one side and all
                > other browsers on the other side.[/color]

                No, you don't have to worry about the browser. If you use a CSS
                definition that the browser doesn't understand, it ignores it. Meaning,
                when any browser that encounters div { myOffTheWallAtt ribute: 0px;} it
                will simply ignore it.

                [color=blue]
                > For the moment (where is no IE 7.x) I would do this in the following way:[/color]

                And you would fail.
                [color=blue]
                > var strBVersion = new String(navigato r.appVersion);
                > var bVersion = str.indexOf("MS IE 6");
                > if (bName == "Microsoft Internet Explorer" && bVersion != -1)
                > // IE 6.x
                > else
                > // some IE older than IE 6 or none IE browsers
                >[/color]

                You corrected the str. mistake, but where is bName defined?

                This is the navigator.appVe rsion as reported by AOL 9.0, which passes
                your test but it is *not* IE6.0

                4.0 (compatible; MSIE 6.0; AOL 9.0; Windows NT 5.1; SV1)

                I think what you were after was the appName, but this is what AOL reports:

                Microsoft Internet Explorer

                Again, its not Internet Explorer, but a shell of it. In any event, its
                irrelevant to the question. That is the same appName as given by Opera
                7 in IE spoof mode.
                [color=blue]
                > But it could be (which is very presumably) an IE 7.x or something like that.
                > To support also this one, you should better modify the above instructions in
                > the following way to support all IE 6.x or higher versions - so a more
                > robust query would be:
                >
                > var strBVersion = new String(navigato r.appVersion);
                > var bVersion1 = str.indexOf("MS IE 6");
                > var bVersion2 = str.indexOf("MS IE 7");
                > if (bName == "Microsoft Internet Explorer" && bVersion1 != -1 && (bVersion1
                > != -1 || bVersion2 != -1))
                > // IE 6.x or higher
                > else
                > // some IE older than IE 6 or none IE browser[/color]

                And it still won't work. See above.

                --
                Randy
                comp.lang.javas cript FAQ - http://jibbering.com/faq

                Comment

                • Richard Cornford

                  #9
                  Re: Browser version detection: How?

                  Thomas Hoheneder wrote:[color=blue][color=green]
                  >> Yep, I defined a 'overflow-y'.
                  >> As far as I know IE 6 is the only browers which
                  >> supports this. (Am I correct?)[/color][/color]

                  IE 5.0 supports overflow-x/y (and IE 4 may also).

                  <snip>[color=blue]
                  > ... I would do this in the following way:
                  >
                  > var strBVersion = new String(navigato r.appVersion);
                  > var bVersion = str.indexOf("MS IE 6");
                  > if (bName == "Microsoft Internet Explorer" && bVersion != -1)
                  > // IE 6.x
                  > else
                  > // some IE older than IE 6 or none IE browsers
                  >
                  > But it could be (which is very presumably) an IE 7.x or
                  > something like that. To support also this one, you should
                  > better modify the above instructions in the following way
                  > to support all IE 6.x or higher versions - so a more robust
                  > query would be:[/color]

                  It has not been Microsoft's style to remove anything form IE once they
                  have introduced it, so overflow-x/y are likely to remain part of their
                  browser in future versions.
                  [color=blue]
                  > var strBVersion = new String(navigato r.appVersion);
                  > var bVersion1 = str.indexOf("MS IE 6");
                  > var bVersion2 = str.indexOf("MS IE 7");[/color]

                  It might be another 2 years before we see the next IE version, but after
                  that they may produce a number of versions in rapid succession. So they
                  may be on IE 8 within a year of IE 7 (assuming they decide to use
                  sequential version numbers instead of some naming or coding system, as
                  appears to be recent fashion).

                  One of the more obvious flaws in browser detection is the anticipation
                  of future versions of browsers. The assumption that the next version of
                  IE will be identifiable as "MSIE 7", even if true, will still mean that
                  this code will need maintenance when the following version comes out.
                  [color=blue]
                  > if (bName == "Microsoft Internet Explorer" && bVersion1 != -1 &&
                  > (bVersion1 != -1 || bVersion2 != -1))
                  > // IE 6.x or higher
                  > else
                  > // some IE older than IE 6 or none IE browser
                  >
                  > Hope this helps you.[/color]

                  It never helps to be recommending browser detection in any form, but
                  particularly based on properties of the - navigator - object. The
                  assumed relationships required for this style of detection are not even
                  true for current browsers. This particular 'test' will have
                  misidentified IceBrowser and NetFront as IE 6 by default, and may also
                  end up treating half a dozen other browsers as IE 6 depending on user
                  preference settings. It is really unrelated to the problem it is trying
                  to solve and as a result will be wide of the mark much of the time.

                  Feature detection testing is based around trying to determine support
                  for any particular feature of web browsers in a way that is closely
                  related to the problem (preferably by a one-to-one relationship). In
                  this case the question of interest is; does the browser support the
                  overflow-x/y style properties (and has nothing to do with whether that
                  browser actually is an IE version or not). That question might best be
                  answered by examining a - style - object, be it the style object
                  belonging to some DOM element, or retrieved from a style rule in the -
                  document.styleS heets - object. A browser that supports overflow-x/y - is
                  likely to manifest - overflowX/Y - properties on its - style - objects,
                  while a browser lacking support almost certainly will not. So, given a
                  reference to a - style - object, a discriminating feature detection test
                  that has a clear direct relationship with the browser's support for the
                  feature of interest would be:-

                  if( typeof aStyleObject.ov erflowY == 'string'){
                  ... // browser can be expected to support overflow-y.
                  }

                  And like feature detection testing in general, not only does it answer
                  the question that needs to be answered (rather than some other question
                  that is assumed to be related to the first in some (often invalid) way),
                  it is also future proof as it no longer matters whether the next (or
                  subsequent) version of IE supports the feature (or indeed whether the
                  browser is a Microsoft browser at all). So instead of writing code that
                  you know will require future maintenance (when IE 8 is released) you are
                  writing code that stands a good chance of never needing looking at again
                  (once it is properly tested).

                  Of course CSS problems are best tacked with CSS techniques (as Randy
                  mentioned) rather than scripting.

                  Richard.


                  Comment

                  Working...