1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Detecting Browsers with JavaScript

Discussion in 'JavaScript and AJAX' started by pradeep, Nov 16, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Many times we need to write certain parts of our script according to the browser, i.e. we need to detect the browser. Here's a snippet of how to do it.

    Code:
    var userAgent = navigator.userAgent.toLowerCase();
    var is_opera  = (userAgent.indexOf('opera') != -1);
    var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
    var is_webtv  = (userAgent.indexOf('webtv') != -1);
    var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
    var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
    var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
    var is_kon    = (userAgent.indexOf('konqueror') != -1);
    var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
    var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
     
  2. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    how to embed this in html page
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Inside a <script> tag of course, or you can put it in a external javascript file and call that file in your HTML page.
     

Share This Page