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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    decent browser detection

    I know this is probably a stupid question which is asked a million times... but anyone got a good reliable browser detection script? I've written the following:

    Code:
    var useragent = navigator.userAgent;
    var browser_name = navigator.appName;
    		
    if (useragent.indexOf('MSIE') > -1) { alert("we have IE"); }
    if ((useragent.indexOf('Mozilla') > -1) && (useragent.indexOf('MSIE') < 0)) { alert("we have mozilla!"); }
    but I'm not sure it is fool proof!!
    Cheers...

  • #2
    Regular Coder
    Join Date
    Aug 2003
    Location
    Loughborough University
    Posts
    427
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There are many many browsers out there, you cannot possibly detect them all so why bother trying? You were right, the script you have is not fool-proof, it will group Opera into the same category as IE.

    Why do you need to detect browsers anyway? There are very few (if any) scripts that you actually need to detect browsers for.
    AMD Athlon "Barton" XP 3200+ (11*200.5MHz = 2.2GHz)
    BFG GeForce 6800 Ultra OC 256Mb
    3 * Kingston 256Mb PC3200 DDR400
    Seagate Barracuda 120Gb Ultra ATA-100 and Seagate Barracuda 160Gb, SATA-150

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    I've got a free WYSIWYG editor (www.textarearich.com) and I need to now make a version which will work in Mozilla... I have the code, I just need to sniff for either IE or Mozilla - any others I can throw out for now cos I cant code for them yet!!

  • #4
    Regular Coder
    Join Date
    Aug 2003
    Location
    Loughborough University
    Posts
    427
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's not really the best way to go about things, just tweak your layout so that it is acceptable in both browsers. So long as the content is delivered to the user that's all that matters.
    AMD Athlon "Barton" XP 3200+ (11*200.5MHz = 2.2GHz)
    BFG GeForce 6800 Ultra OC 256Mb
    3 * Kingston 256Mb PC3200 DDR400
    Seagate Barracuda 120Gb Ultra ATA-100 and Seagate Barracuda 160Gb, SATA-150

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by lavalamp
    So long as the content is delivered to the user that's all that matters.
    well said. anything else is just frivolous. if it's still needed then, object and/or method detection will iron out browser differences.
    *keep it simple (TM)

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    its for referencing objects and things... it is deffinately needed!

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by homerUK
    its for referencing objects and things
    it's not needed like that tho. in your functions, you just detect what's supported and then do what's necessary. there's no need to do var isIE6=document.all=true or anything like that:

    Code:
    if (document.whatever) do whatever;
    else if (document.somethingElse) do somethingElse;
    *keep it simple (TM)

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    oh right! good thinking...
    I was thinking the code would be something like

    if (browser == IE)
    {
    do this....
    }
    if (browser == Moz)
    {
    do this then..
    }

    glad you suggested that!! cheers!

  • #9
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i used to do what you do. but it's kinda not needed really. the only time i ever needed to explicitly differentiate between browsers was when marking up a system to help users activate cookies, JS, etc, which is different from browser to browser. for day-to-day scripting, tho, it's not needed.
    *keep it simple (TM)

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    jbot: It depends a lot on the type of script you are using. Advanced UI (from a web perspective) can require quite a lot of trickery to get working. Especially when working with form controls, character inputs, style handling and events.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #11
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    jbot: It depends a lot on the type of script you are using. Advanced UI (from a web perspective) can require quite a lot of trickery to get working. Especially when working with form controls, character inputs, style handling and events.
    conceded, hence why i mentioned my browser helper system. but inline object detection should be the first port of call. you can then break it down to appName and appVersion later if need be: it's just best not to rely on it, given UA-spoofing etc.
    *keep it simple (TM)

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know it's perverse, but I use object and feature detection to detect browsers - it's not completely reliable, but it's much better than UA information, for those times when it really is the browser you need to know. http://www.brothercake.com/site/reso...ripts/sniffer/
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

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