jQuery code to detect the browser

I amĀ Using below jQuery code to detect the browser.
It works well for IE, mozilla, Safari and opera browser
/* ===== Firefox ===== */
// Select Firefox
if (jQuery.browser.mozilla) {
    // do sth.
}
// Select Firefox 1.5.x to 2.x
if (jQuery.browser.mozilla && jQuery.browser.version.substr(0, 3) == '1.8') {
    // do sth.
}
// Select Firefox under 3.x
if (jQuery.browser.mozilla && jQuery.browser.version < '1.9') {
    // do sth.
}
// Select Firefox 3.0.x and above
if (jQuery.browser.mozilla && jQuery.browser.version.substr(0, 3) == '1.9') {
    // do sth.
}
// Select just Firefox 2.0.x
if (jQuery.browser.mozilla && jQuery.browser.version == '1.8.1') {
    // do sth.
}
// Select just Firefox 3.0.x
if (jQuery.browser.mozilla && jQuery.browser.version == '1.9') {
    // do sth.
}
// Select just Firefox 3.5.x
if (jQuery.browser.mozilla && jQuery.browser.version == '1.9.1') {
    // do sth.
}
// Select just Firefox 3.6.x
if (jQuery.browser.mozilla && jQuery.browser.version == '1.9.2') {
    // do sth.
}
/* ===== Internet Explorer ===== */
// Select Internet Explorer
if (jQuery.browser.msie) {
    // do sth.
}
// Select Internet Explorer above 6
if (jQuery.browser.msie && jQuery.browser.version > 6) {
    // do sth.
}
// Select Internet Explorer 7 and below
if (jQuery.browser.msie && jQuery.browser.version <= 7) {
    // do sth.
}
// Select just Internet Explorer 6
if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
    // do sth.
}
// Select just Internet Explorer 7
if (jQuery.browser.msie && jQuery.browser.version == '7.0') {
    // do sth.
}
// Select just Internet Explorer 8
if (jQuery.browser.msie && jQuery.browser.version == '8.0') {
    // do sth.
}
// Select just Internet Explorer 9
if (jQuery.browser.msie && jQuery.browser.version == '9.0') {
    // do sth.
}
/* ===== Safari / Chrome ===== */
// Select Safari / Chrome
if (jQuery.browser.safari) {
    // do sth.
}
// Select Safari 3
if (jQuery.browser.safari && (navigator.appVersion.indexOf('3.') != -1)) {
    // do sth.
}
// Select Safari 4
if (jQuery.browser.safari && (navigator.appVersion.indexOf('4.') != -1)) {
    // do sth.
}
// Select Chrome 1
if (jQuery.browser.safari && (navigator.appVersion.indexOf('1.') != -1)) {
    // do sth.
}
// Select Chrome 3
if (jQuery.browser.safari && (navigator.appVersion.indexOf('3.') != -1)) {
    // do sth.
}
/* ===== Opera ===== */
// Select Opera
if (jQuery.browser.opera) {
    // do sth.
}
// Select Opera 9.5 and above
if (jQuery.browser.opera && jQuery.browser.version >= '9.5') {
    // do sth.
}
// Select just Opera 9.5
if (jQuery.browser.opera && jQuery.browser.version == '9.5') {
    // do sth.
}
// Select just Opera 10
// Note: Opera 10's user agent looks like Opera/9.80 [...] Version/10.00
if (jQuery.browser.opera && jQuery.browser.version == '9.8') {
    // do sth.
}
Share this nice post:

One thought on “jQuery code to detect the browser

Leave a Reply

Your email address will not be published. Required fields are marked *