PeaceDealer
PeaceDealer

Reputation: 1017

Browser detection now that $.browser doesn't work anymore

Since $.browser now have been removed from jQuery's 1.9, I'm looking for similar methods of detecting browsers?

Tried to use php's get_brower, but it allways returned as "Default Browser".

Update: I looked at jquery.support and it did not provide me with any usefull information

Upvotes: 2

Views: 212

Answers (5)

hexalys
hexalys

Reputation: 5257

I too miss the $.browser detection of jQuery. While I understand that $.support makes more sense for many cases, I initially used browser detection to get a css prefix property, which can make more sense when it comes to apply conditional styling, instead of using all 3 prefixes. Or even add the -pie- prefix for IE 6-9 which is solely based on IE's version.

Here is the model for the function I'll be using, adapted from jQuery's deprecated $.browser, if that can help.

nav = navigation;
nav.detectBrowser = function() {
var t = this,
    a = this.userAgent.toLowerCase(),
    match = /(chrome)[ \/]([\w.]+)/.exec(a) ||
    /(webkit)[ \/]([\w.]+)/.exec(a) ||
    /(firefox)[ \/]([\w.]+)/.exec(a) ||
    /(msie) ([\w.]+)/.exec(a) ||
    /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(a) ||
    a.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(a) || [];

t.browser = match[1] || false;
t.version = match[2] || "0";
if (t.browser) t[match[1]] = true;
if (t.msie)
    t.ie = parseInt(t.version);//ie main version or false if not IE
else if (t.chrome)
    t.webkit = true;//chrome is webkit
else if (t.webkit)
    t.safari = true;
    t.pre = t.webkit ? '-webkit-' : t.firefox ? '-moz-' : t.ie > 7
        ? '-ms-' : t.opera ? '-o-' : '';//css prefix
}
nav.detectBrowser();

Note I use the window.navigation global object for the example, where your old $.browser object would now be 'nav' or 'window.nav'. nav.pre is the browser prefix for css3 features and nav.ie is the integer of the Internet Explorer version if it's IE.

Upvotes: 0

chriz
chriz

Reputation: 1580

You can detect Internet explorer like so:

<!--[if IE]>
<![endif]-->

Mobile browsers can be detected with this tool: http://detectmobilebrowsers.com/

Upvotes: -1

Alex
Alex

Reputation: 10226

As you pointed out, searching through the userAgent may not be reliable. But basically that's what you have to do if you dont want to use feature detection. The rest is said here already

Upvotes: 0

Dave
Dave

Reputation: 3288

<?php 
 echo $_SERVER['HTTP_USER_AGENT'];
?> 

And then either select case's or if's or regex the results for what you want.

Upvotes: 0

Quentin
Quentin

Reputation: 944021

From the documentation for $.browser:

use feature detection instead (see jQuery.support)

Upvotes: 3

Related Questions