SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    NY
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript--browser sniff

    http://home.earthlink.net/~nomorabito/index.html

    Making a website, I found out it looks different on different machines, Mac, and PC, and then on different versions of browsers.
    This led to trying to find a solution to this, as the page was written with no technical expertise at all, just hit and miss, starting from a web page example, as I am no programmer.

    After much help, and input, it was rewritten in CSS, by a kind person in these forums, and thru pm's, and other threads, some of which I didin't really understand, it seems a good course for me to follow would be to have the site in CSS for older browsers, as it renders ok in Netscape 7.1 Mac, and PC, IE 5.2 Mac, and IE 5.5 PC, and Firefox 0.8,

    So, again in these forums (they're great!), www.browsercam.com, lets you see your page on different versions of browsers, etc., and Iwill be using free trial, so after seeing where it's messed up, I need a javascript that will
    tell me the browser someone is using so that they can view the regular, or CSS version of the page.

    I tried looking it up, and found this


    <html>
    <body>
    <script type="text/javascript">
    document.write("<p>Browser: ")
    document.write(navigator.appName + "</p>")

    document.write("<p>Browserversion: ")
    document.write(navigator.appVersion + "</p>")


    </script>
    </body>
    </html>
    But realised this really didn't tell me what browser they were using--

    That would be found out by:

    document.write("UA=" + x.userAgent)
    document.write("<br />")
    But was part of a string, and then I'm lost, and I understand these only basically..

    --Would someone please give me a script that would tell what browser version someone was using and how to direct them to one of two pages?

    Thanks for all the help--these forums are GREAT!
    Bob

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The 'Ultimate' browser sniffer can be found somewhere on http://devedge.netscape.com

    redirection is simply a matter of if/elses:

    if (ie4) location = 'ie4page.html'
    else if (nn4) location = 'nn4page.html'
    else location = 'version5page.html'

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    NY
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vincent Puglia
    The 'Ultimate' browser sniffer can be found somewhere on http://devedge.netscape.com

    redirection is simply a matter of if/elses:

    if (ie4) location = 'ie4page.html'
    else if (nn4) location = 'nn4page.html'
    else location = 'version5page.html'

    Vinny
    Hi, Vinny--

    Thanks so much for replying!

    I think this is the link--

    http://devedge.netscape.com/viewsour...ser-detection/

    It's way beyond me--!!
    Thanks, Bob

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Bob,

    Actually, that is a better page; the one I meant was simply some 100 lines of code -- which probably would have been a lot more over your head.

    The important thing to note in the link you provided is that it mentions browser detection is best done by checking for objects and properties -- which ultimately is the best technique to use. For example:
    PHP Code:
    if (document.getElementById)
       ...
    code used by all version 5browsers
    else if (document.all)
      ...
    code used by IE4...
    else if (
    document.layers)
      ...
    code used by NN4...
    else
      ...
    everyone else.... 
    In fact, in many pages you will see code like:

    IE4 = document.all
    IE5 = document.all && document.getElementById
    Opera = document.getElementById && ! document.all

    and then something like:

    if (IE5 && Opera) ....

    If you really want to use version detection, the following is what I used to use when I first started:
    PHP Code:
    var agt=navigator.userAgent.toLowerCase();
    var 
    isMajor  parseInt(navigator.appVersion);
    var 
    isIE    = (agt.indexOf("msie") != -1);
    var 
    isIE4   = (isIE && (isMajor == 4) && (agt.indexOf("msie 5.0")==-1) ); 
    var 
    isIE4up = (isIE  && (isMajor >= 4));
    var 
    isAOL   = (agt.indexOf("aol") != -1);
    var 
    isAOL4  = (isAOL && isIE4);
    var 
    isOk    = (isIE4up || isAOL4); 
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    NY
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vincent Puglia
    Hi Bob,

    Actually, that is a better page; the one I meant was simply some 100 lines of code -- which probably would have been a lot more over your head.

    The important thing to note in the link you provided is that it mentions browser detection is best done by checking for objects and properties -- which ultimately is the best technique to use. For example:
    PHP Code:
    if (document.getElementById)
       ...
    code used by all version 5browsers
    else if (document.all)
      ...
    code used by IE4...
    else if (
    document.layers)
      ...
    code used by NN4...
    else
      ...
    everyone else.... 
    In fact, in many pages you will see code like:

    IE4 = document.all
    IE5 = document.all && document.getElementById
    Opera = document.getElementById && ! document.all

    and then something like:

    if (IE5 && Opera) ....

    If you really want to use version detection, the following is what I used to use when I first started:
    PHP Code:
    var agt=navigator.userAgent.toLowerCase();
    var 
    isMajor  parseInt(navigator.appVersion);
    var 
    isIE    = (agt.indexOf("msie") != -1);
    var 
    isIE4   = (isIE && (isMajor == 4) && (agt.indexOf("msie 5.0")==-1) ); 
    var 
    isIE4up = (isIE  && (isMajor >= 4));
    var 
    isAOL   = (agt.indexOf("aol") != -1);
    var 
    isAOL4  = (isAOL && isIE4);
    var 
    isOk    = (isIE4up || isAOL4); 
    Vinny
    Hi, Vinny--thanks again for all your help!!!
    Bob


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
  •