dcsimg
Results 1 to 19 of 19

Thread: Determining which web browser using Javascript?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Determining which web browser using Javascript?

    I know part of web development is being able to view a website using any browser. One way I've heard is by using Javascript to detect browser and then loading a Cascading StyleSheet based on that browser. There are at least 6 major browsers on the market today. So essentially you could have 6 different cascading stylesheets to load. If there is a better way, can someone please share this method.

    Thanks so much,
    Blake

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,747

    Re: Determining which web browser using Javascript?

    The user agent has that information - I store that in a LOGIN DETAIL table for each connection to my web app - here are the last couple of dozen.

    It is stored in this variable - navigator.userAgent

    Code:
    DoMobile	DetailText
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8
    0	Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    1	Mozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) CriOS/68.0.3440.83 Mobile/15E216 Safari/604.1
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    1	Mozilla/5.0 (Linux; Android 7.0; LGL84VL Build/NRD90U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.91 Mobile Safari/537.36
    1	Mozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1 Safari/605.1.15
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0
    1	Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1
    0	Mozilla/5.0 (X11; CrOS x86_64 10895.21.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.35 Safari/537.36
    1	Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15
    0	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
    0	Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    848

    Re: Determining which web browser using Javascript?

    Rather than detecting the browser it is probably better to detect the capabilities that the current browser has, using Javascript a library like https://modernizr.com/ might be a good option, in terms of appearance then canIuse.com is a good reference as to what browsers can do what but you might want to automate generating certain css vendor specific entries with somethign like https://github.com/postcss/autoprefixer

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,788

    Re: Determining which web browser using Javascript?

    The best option is to develop your site so that it works in any browser without any conditional code. It's not always possible but you should certainly aim to do so. As long as you don't have to support really old versions, the lowest common denominator isn't necessarily all that low. Only add conditional code if you absolutely must.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: Determining which web browser using Javascript?

    Looking at your example, how do you extract the browser. See attached image.
    Attached Images Attached Images  
    Blake

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: Determining which web browser using Javascript?

    I figured that would be the best way but I'm far from being a guru when it comes to CSS & Javascript. Consequently, I don't know enough to cover all the little chinks in the armor. I have a personal website that is pretty close using one CSS file but it's not full-proof! It still doesn't render correctly for all the browsers.
    Blake

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,747

    Re: Determining which web browser using Javascript?

    I'm using REGEX to determine mobile vs non-mobile - which is also NOT recommended!

    I found the code online and so far it's been working (900 kids showing up for college in Texas filling out room inventory and maintenance request forms).

    I'm storing the USER AGENT in case I find situations where students are not getting the correct mobile version of the HTML. That way I can debug why.

    I've personally never needed to know the browser. I use the jQuery library and one of the main reasons it exists is so I don't have to worry about browser versions. I'm several versions behind with my jQuery - I'm going to have to get current sometime in the near future.

    What are your requirements that sent you down the road of doing browser sniffing??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: Determining which web browser using Javascript?

    I'm new in the Web Development world. I just figured it makes sense for your web app to be viewable in every browser.
    Blake

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,788

    Re: Determining which web browser using Javascript?

    Quote Originally Posted by blakemckenna View Post
    I just figured it makes sense for your web app to be viewable in every browser.
    Don't assume that it won't be. Things are still not perfect but they're not as bad as they used to be.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,747

    Re: Determining which web browser using Javascript?

    Quote Originally Posted by blakemckenna View Post
    Looking at your example, how do you extract the browser. See attached image.
    Just cut and paste that line into GOOGLE and your first link will be a site called something like WHATISMYUSERAGENT

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: Determining which web browser using Javascript?

    I tried this and it didn't come up!

    Just cut and paste that line into GOOGLE and your first link will be a site called something like WHATISMYUSERAGENT
    Blake

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: Determining which web browser using Javascript?

    Just cut and paste that line into GOOGLE and your first link will be a site called something like WHATISMYUSERAGENT
    I tried this and it didn't come up!
    Blake

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,747

    Re: Determining which web browser using Javascript?

    I pasted the WHOLE user agent that you highlighted in your image - just the USER AGENT string - not that 0 MOBILE flag floating out front.

    First search results was this link

    http://www.whatismyuseragent.net/use...+Safari/537.36

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: Determining which web browser using Javascript?

    This website is basically showing me what your storing in your LOGIN DETAIL file. Forgive me but I'm not seeing how this is useful. Sometimes I can't see the forest through the trees!
    Blake

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,747

    Re: Determining which web browser using Javascript?

    That page tells me the USER AGENT FAMILY is FIREFOX.

    For some reason that FAMILY was determined - even though apparently conflicting info is in that string.

    I think you will be going down a huge rabbit hole trying to take the USER AGENT to then determine the exact browser. The research needed to understand the history and relationships of various browser engine internals.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: Determining which web browser using Javascript?

    Ok, so in short...there is NO sure-fire way to determine which browser is rendering a page...at least that's what I'm hearing.
    Blake

  17. #17
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    848

    Re: Determining which web browser using Javascript?

    Quote Originally Posted by blakemckenna View Post
    Ok, so in short...there is NO sure-fire way to determine which browser is rendering a page...at least that's what I'm hearing.
    Most browsers can also be made to lie about the user agent string as well, don't rely on detecting the browser - try detecting capabilities.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: Determining which web browser using Javascript?

    Detecting capabilities??? Can you please elaborate on that?
    Blake

  19. #19
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,747

    Re: Determining which web browser using Javascript?

    Quote Originally Posted by blakemckenna View Post
    Detecting capabilities??? Can you please elaborate on that?
    Post #3 has a link for a modernizer library that does just that.

    And also read that link to understand how this also is not a perfect solution (note the part about detecting touch screen).

    It is almost like the browser manufacturers are working against us developers!

    https://modernizr.com/docs/#using-mo...ith-javascript

    Touch Events touchevents

    Indicates if the browser supports the W3C Touch Events API.

    This does not necessarily reflect a touchscreen device:

    Older touchscreen devices only emulate mouse events
    Modern IE touch devices implement the Pointer Events API instead: use Modernizr.pointerevents to detect support for that
    Some browsers & OS setups may enable touch APIs when no touchscreen is connected
    Future browsers may implement other event models for touch interactions

    See this article: You Can't Detect A Touchscreen.

    It's recommended to bind both mouse and touch/pointer events simultaneously – see this HTML5 Rocks tutorial.

    This test will also return true for Firefox 4 Multitouch support.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width