SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot shoorace's Avatar
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Here is browser detection code in JavaScript

    Simple JavaScript code for browser detection:

    Code JavaScript:
    /**
     * Browser detection
     * @Created-On 2007-11-27 23:46:51
     */
    function detectBrowser(){
    	if(navigator.userAgent.indexOf("Opera")!=-1){
    		return "Opera";
    	}else if(navigator.userAgent.indexOf("MSIE")!=-1){
    		return "MSIE";
    	}else if(navigator.userAgent.indexOf("Navigator")!=-1){
    		return "Netscape";
    	}else if(navigator.userAgent.indexOf("Firefox")!=-1){
    		return "Firefox";
    	}else if(navigator.userAgent.indexOf("Safari")!=-1){
    		return "Safari";
    	}
    }

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    About 15 years out of date.

    The userAgent is a free format field that can be set to contain ANYTHING AT ALL by the browser's owner - at least it can with Internet Explorer, Firefox, Mozilla, and Netscape. Opera changes the userAgent automatically to whatever you want to present to different web sitesw with a choice of identifying itself as IE, Firefox, or Opera.

    Using code like that is the easiest way to totally stuff up your web page.

    Use feature sensing when necessary in your code - NEVER test user enterable free format fields to try to work out what browser it is.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    That code might have been alright in 1990 but not in the year 2000.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Location
    Sydney, Australia
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My Favourite way of doing this :-)
    Code JavaScript:
    if (document.all && !window.opera)
    {
           // Internet Explorer
    }
    else if (window.opera)
    {
           // Opera
    }
    else if (navigator.userAgent.indexOf("Safari") != -1)
    {
           // Safari
    }
    else if (!document.all && !window.opera)
    {
           // Firefox/Mozilla, Netscape and other browsers
    }
    Im sure it can be improved upon, especially the recognition of safari.
    Kind Regards,
    Steve
    http://www.rtepad.com

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    That code might have been alright in 1990 but not in the year 2000.
    Yes, 100% right.
    And I use this code at 1990; quote from DHML books,
    which is hot at that time.
    matt

  6. #6
    SitePoint Zealot shoorace's Avatar
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, what is the way of coding to detecting browser in current age???

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    You don't need to know which of the several differnt browsers that it is, you just need to know if it supports the code you weant to run.

    For example to test if a browser supports Document Object Model access via the standard getElementById() method you use:

    if (document.getElementById) {
    alert('browser supports getElementById');
    } else {
    alert('antiquated browser that doesn't understand 21st century code');
    }


    You do the same thing for any code where a browser may or may not support the a given command - test if the browser recognises the command and if so then you know you can use it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Zealot shoorace's Avatar
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, actually I am in need to give width to overlay div, and found that different browsers have different width in px, due to which scrollbar is generated....

  9. #9
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That, shoorace, would be a CSS issue, not a JavaScript issue. Why don't you start a thread in the CSS board (and then link this thread to it in the new one) so we can check it out?

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The scrollbar widths can all be changed from the operating system configuration so there is no guarantee of a particular width for a given browser making browser detection completely pointless for this anyway. A CSS solution will work even when JavaScript is disabled.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •