Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts

    Code to check browser

    I'll be the first to admit, I am not skilled with javascript, but I can do a little work here and there. I remember learning there is a way to detect a user's/viewer's browser. Is there a possible javascript that allows me to detect a viewer's browser, the based on the browser, selects css/html to use so that the website is viewed correctly?
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Old Pedant, I remember seeing that, but would you happen to know a code that doesn't write it but instead transfers it to another javascript that will display the correct layout.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    ???

    Code:
    var agent = navigator.userAgent.toLowerCase(); // lower case optional, but easier
    if ( agent.indexOf("firefox") >= 0 ) 
    {
        ... do something for firefox ...
    } else if ( agent.indexOf("msie 8") >= 0 ) {
        ... for msie 8 ...
    } else if ( agent.indexOf("msie 9") >= 0 ) {
        ... for msie 9 ...
    } else if ( agent.indexOf("msie") >= 0 ) {
        ... for other msie ...
    } ... etc. ...
    what code you use for each browser has to be up to you.

    Now, personally, I much prefer trying to write HTML that works the same in all browsers.

    Unless you are doing something reasonably esoteric, it isn't all *THAT* hard.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    I tried to do what I am doing for IE but if I mess with the code it messes up the other browsers.. IE is the only thing irritating me.. as always..
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Browser detection using navigator.userAgent is unreliable.

    To detect IE:-

    Code:
    <script type = "text/javascript">
    
    var IE = ("v"=="\v");
    alert (IE);   // true or false
    
    </script>

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Quote Originally Posted by Philip M View Post
    Browser detection using navigator.userAgent is unreliable.
    Well, not to mention slower and clumsier.

    Yes, if ALL you are concerned with is detecting IE, clearly Philip's answer is far superior. And sneakier.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    An added benefit is that IE9 does define the vertical tab escape, unlike the previous versions.
    Philip has a simple way to separate any code that needs older IE's event or css treatments.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by mrhoo View Post
    An added benefit is that IE9 does define the vertical tab escape, unlike the previous versions.
    Philip has a simple way to separate any code that needs older IE's event or css treatments.
    I have not got to IE9 yet.

    Try this (I think it is Kor's)

    Code:
    <script type = "text/javascript">
    var IE = /*@cc_on!@*/false;
    alert (IE);
    </script>

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Thanks, Philip, but I meant that IE 9 does not need to be handled with the older IE's- the same code that works in all the other browsers works fine in IE9,
    including the newer Array methods and event and css styleSheet handling.

    So your method separates the IE versions that need special code from the browsers that can run a 'standard' code..

  • Users who have thanked mrhoo for this post:

    Philip M (11-11-2010)


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •