Monday, December 8, 2014

Browser Detection with Jquery

Browser detection with Jquery

In this post, find JavaScript/jQuery code to detect various type and version of web browser. Below jQuery code gives you an idea about $.browser property.


For Chrome and Safari

if ($.browser.safari) {
    // code goes here
}

 
Select Safari 3
if ($.browser.safari && (navigator.appVersion.indexOf('3.') != -1)) {
    // code goes here
}

 
Select Safari 4
if ($.browser.safari && (navigator.appVersion.indexOf('4.') != -1)) {
   // code goes here
}

 
Select Chrome 1
if ($.browser.safari && (navigator.appVersion.indexOf('1.') != -1)) {
    // code goes here
}

 
Select Chrome 3
if ($.browser.safari && (navigator.appVersion.indexOf('3.') != -1)) {
    // code goes here
}


Select Chrome 13
if ($.browser.safari && (navigator.appVersion.indexOf('13.') != -1)) {
    // code goes here
}


Select Chrome 16
if ($.browser.safari && (navigator.appVersion.indexOf('16.') != -1)) {
   // code goes here
}
 

For Firefox 

if ($.browser.mozilla) {
    // code goes here
}

 
Select Firefox 1.5.x to 2.x
if ($.browser.mozilla && $.browser.version.substr(0, 3) == '1.8') {
    // code goes here
}

 
Select Firefox under 3.x
if ($.browser.mozilla && $.browser.version < '1.9') {
    // code goes here
}

 
Select Firefox 3.0.x and above
if ($.browser.mozilla && $.browser.version.substr(0, 3) == '1.9') {
   // code goes here
}

 
Select just Firefox 2.0.x
if ($.browser.mozilla && $.browser.version == '1.8.1') {
   // code goes here
}

 
Select just Firefox 3.0.x
if ($.browser.mozilla && $.browser.version == '1.9') {
   // code goes here.
}

 
Select just Firefox 3.5.x
if ($.browser.mozilla && $.browser.version == '1.9.1') {
    // code goes here
}

 
Select just Firefox 3.6.x
if ($.browser.mozilla && $.browser.version == '1.9.2') {
    // code goes here
}

 
Select just Firefox 7.x
if ($.browser.mozilla && $.browser.version.substr(0, 1) == '7') {
    // code goes here
}

 
Select just Firefox 14.x
if ($.browser.mozilla && $.browser.version.substr(0, 2) == '14') {
    // code goes here
}
  

For Opera

if ($.browser.opera) {
    // code goes here.
}

 
Select Opera 9.5 and above
if ($.browser.opera && $.browser.version >= '9.5') {
   // code goes here
}

 
Select just Opera 9.5
if ($.browser.opera && $.browser.version == '9.5') {
    // code goes here
}

 
Select just Opera 10
if ($.browser.opera && $.browser.version == '9.8') {
    // code goes here
}


For Internet Explorer (IE sucks)

if ($.browser.msie) {
    // code goes here
}

 
Select Internet Explorer above 6
if ($.browser.msie && $.browser.version > 6) {
    // code goes here
}

 
Select Internet Explorer 7 and below
if ($.browser.msie && $.browser.version <= 7) {
   // code goes here
}

 
Select just Internet Explorer 6
if ($.browser.msie && $.browser.version == '6.0') {
    // code goes here
}

 
Select just Internet Explorer 7
if ($.browser.msie && $.browser.version == '7.0') {
    // code goes here.
}

 
Select just Internet Explorer 8
if ($.browser.msie && $.browser.version == '8.0') {
    // code goes here
}

 
Select just Internet Explorer 9
if ($.browser.msie && $.browser.version == '9.0') {
    // code goes here.
}


Note: release of jQuery 1.9 $.browser feature was removed. But to support legacy code, they have released jQuery Migrate plugin to detect deprecated and removed features, or to restore old features for those sticky situations where you need old code to run with new jQuery. 

2 comments:

  1. I ADAMS KEVIN, a representative Aiico Insurance plc, we trust and respect for individual differences in day out a loan. We will provide 2% of the loan's interest rate. If you are interested in this business contact us by e-mail: (adams.credi@gmail.com) now transfer their loan documents issued properly. Do you need a loan to set up business or school if you are very welcome to Aiico Insurance plc. You can also contact us by e-mail: (adams.credi@gmail.com). We first week can request a balance transfer.

    DO YOU NEED LOAN FOR PERSONAL BUSINESS? IF YOU CONTACT YOUR EMAIL ABOVE TO PROCEED WITH YOUR LOAN TRANSFER IMMEDIATELY OK.

    ReplyDelete
  2. Wonderful work! This is the type of info that should be shared around the internet. Shame on Google for not positioning this post higher! Come on over and visit my website . Thanks =) excel programmierung berlin

    ReplyDelete