2

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

hexalys
  • 4,898
  • 3
  • 26
  • 54
PeaceDealer
  • 739
  • 1
  • 7
  • 15
  • 3
    http://api.jquery.com/jQuery.browser/: **We recommend against using this property; please try to use feature detection instead (see jQuery.support).** – deceze Feb 26 '13 at 16:40
  • Why not do feature detection instead of browser detection? – Jon Feb 26 '13 at 16:40
  • I looked at this, but i cannot find anything in there that helps me. I need to find out exactly what browser the user is using – PeaceDealer Feb 26 '13 at 16:42
  • then you might want to search trough window.navigator.userAgent; – Alex Feb 26 '13 at 16:43
  • what exactly do you need browser detection for? Perhaps we can help you find an alternative? – SDC Feb 26 '13 at 16:46
  • @Alex - This just gives me a lot of information, and the browser used changes place. Also learned that information provided with this can often be unreliable. – PeaceDealer Feb 26 '13 at 16:46
  • @PeaceDealer - *any* browser detection, including jQuery's, is unreliable for the same reason. That's one of the reasons it's not recommended. – SDC Feb 26 '13 at 16:47
  • @SDC - It is because I've made some CSS3 and HTML5 objects that is not supported by any version if Internet Explorere – PeaceDealer Feb 26 '13 at 16:47
  • 1
    @PeaceDealer - well, that's easy. Use feature detection for those features. Browser detection is *not* required for this. What features have you used? – SDC Feb 26 '13 at 16:48
  • @SDC - Now we're going into this world of technical specifications. In ie, many images have an annoying border around them, which i did try to remove using more CSS, but didnt work. Fonts don't seem to work proberly either, and some of the margins are very messed up. – PeaceDealer Feb 26 '13 at 16:50
  • @PeaceDealer - well, that sounds like you're getting IE into Quirksmode, which is a whole different issue, and nothing to do with what the browser supports -- Fix that by putting a valid doctype at the top of your page. – SDC Feb 26 '13 at 16:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25166/discussion-between-peacedealer-and-sdc) – PeaceDealer Feb 26 '13 at 16:55
  • 2
    "not supported by any version of Internet Explorer" are you sure? IE has began to support more and more with each version, IE9 already supported most things that you would need `$.browser` for, and IE10 takes that even further. Your images having a border, just add this css style: `a img { border:0px }`. As far as margins, there is a `$.support` for the box model which causes the margin issues. Test for `$.support.boxModel`, if it's false, do your margin fixes. – Kevin B Feb 26 '13 at 16:55
  • @PeaceDealer jQuery suggest use of the script - http://modernizr.com/docs/ – user1428716 Feb 26 '13 at 16:56
  • @PeaceDealer, for your specific situation, you might also want to look into normalizing instead of conditionally using these features: http://necolas.github.io/normalize.css/ – Noyo Apr 13 '13 at 10:52

5 Answers5

3

From the documentation for $.browser:

use feature detection instead (see jQuery.support)

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • I looked at this, but can find nothing to help me in there. From what i gather, it is just to test if specefic features are enabled, how-ever im looking for a method to detect that the user's browser is fx, internet explorer or FireFox – PeaceDealer Feb 26 '13 at 16:42
  • 2
    @PeaceDealer: browser detection is never as good as feature detection. Why would you need to care what browser the user is actually running, as long as it supports the feature you needed. – Lie Ryan Feb 26 '13 at 16:46
0
<?php 
 echo $_SERVER['HTTP_USER_AGENT'];
?> 

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

Dave
  • 3,184
  • 2
  • 21
  • 40
0

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

Community
  • 1
  • 1
Alex
  • 9,339
  • 2
  • 28
  • 46
0

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.

hexalys
  • 4,898
  • 3
  • 26
  • 54
-1

You can detect Internet explorer like so:

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

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

chriz
  • 1,510
  • 18
  • 27