Detect the visitor's browser and browser version

Discussion in 'JavaScript' started by youness_violon, Oct 6, 2008.

  1. #1
    <html>
    <body>
    <script type="text/javascript">
    var browser=navigator.appName;
    var b_version=navigator.appVersion;
    var version=parseFloat(b_version);
    document.write("Browser name: "+ browser);
    document.write("<br />");
    document.write("Browser version: "+ version);
    </script>
    </body>
    </html>


     
    youness_violon, Oct 6, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    you should probably not rely on the headers alone to determine the browser version, google on why and how you can do it instead.

    here is an example from the older version of the mootools 1.11 framework:
    /* Properties:
    	window.ie - will be set to true if the current browser is internet explorer (any).
    	window.ie6 - will be set to true if the current browser is internet explorer 6.
    	window.ie7 - will be set to true if the current browser is internet explorer 7.
    	window.gecko - will be set to true if the current browser is Mozilla/Gecko.
    	window.webkit - will be set to true if the current browser is Safari/Konqueror.
    	window.webkit419 - will be set to true if the current browser is Safari2 / webkit till version 419.
    	window.webkit420 - will be set to true if the current browser is Safari3 (Webkit SVN Build) / webkit over version 419.
    	window.opera - is set to true by opera itself.
    */
    
    window.xpath = !!(document.evaluate);
    if (window.ActiveXObject) window.ie = window[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;
    else if (document.childNodes && !document.all && !navigator.taintEnabled) window.webkit = window[window.xpath ? 'webkit420' : 'webkit419'] = true;
    else if (document.getBoxObjectFor != null) window.gecko = true;
    
    // ... example use
    
    //enable background image cache for internet explorer 6
    if (window.ie6) try {document.execCommand("BackgroundImageCache", false, true);} catch(e){};
    
    PHP:
     
    dimitar christoff, Oct 6, 2008 IP