Easiest/Lightest Replacement For Browser Detection jQuery 1.9?

JqueryBrowser Detection

Jquery Problem Overview


I had several clients complain yesterday that some code stopped working. Apparently it comes down to plug-ins using the now deprecated jQuery.browser which stopped working yesterday when jQuery 1.9 was released.

I (quickly) looked at the 1.9 change docs and it seems like they want me to substitute some pretty heavy libraries just for that one function.

Is there a recommended lightest weight plug-in or code snippet to restore that functionality?

For what these sites need, it's very basic; I only need the most basic detection of IE vs FF vs everyone else.

Suggestions?

Jquery Solutions


Solution 1 - Jquery

I've use the following code answered by Alexx Roche, but i wanted to detect MSIE so:

<script type="text/javascript">
   $(document).ready(function() {
      if (navigator.userAgent.match(/msie/i) ){
        alert('I am an old fashioned Internet Explorer');
      }
   });
</script>

hope it helps!

Solution 2 - Jquery

jQuery Migrate was created to allow for backwards compatibility while you update your code.

https://github.com/jquery/jquery-migrate

As a bonus, it logs deprecated functions as you use them. I would give it a try while you resolve the problems. Also, you should be setting a specific version of jQuery for your sites. It's good to upgrade, but be sure to test those upgrades before putting them in production. If you are using a CDN, you can still specify a specific version in the file name.

Now, you don't need a jQuery plugin for what you are asking. Check out the navigator object.

appCodeName: "Mozilla"
appName: "Netscape"
appVersion: "5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17"
cookieEnabled: true
doNotTrack: null
geolocation: Geolocation
language: "en-US"
mimeTypes: MimeTypeArray
onLine: true
platform: "MacIntel"
plugins: PluginArray
product: "Gecko"
productSub: "20030107"
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17"
vendor: "Google Inc."
vendorSub: ""

Solution 3 - Jquery

var browser = {
		chrome: false,
		mozilla: false,
		opera: false,
		msie: false,
		safari: false
	};
	var sUsrAg = navigator.userAgent;
	if(sUsrAg.indexOf("Chrome") > -1) {
		browser.chrome = true;
	} else if (sUsrAg.indexOf("Safari") > -1) {
		browser.safari = true;
	} else if (sUsrAg.indexOf("Opera") > -1) {
		browser.opera = true;
	} else if (sUsrAg.indexOf("Firefox") > -1) {
		browser.mozilla = true;
	} else if (sUsrAg.indexOf("MSIE") > -1) {
		browser.msie = true;
	}
	console.log(browser.msie);

Solution 4 - Jquery

Put this code in your site (like js file, or after code of jQuery...):

var matched, browser;

// Use of jQuery.browser is frowned upon.
// More details: http://api.jquery.com/jQuery.browser
// jQuery.uaMatch maintained for back-compat
jQuery.uaMatch = function( ua ) {
	ua = ua.toLowerCase();

	var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
		/(webkit)[ \/]([\w.]+)/.exec( ua ) ||
		/(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
		/(msie) ([\w.]+)/.exec( ua ) ||
		ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
		[];

	return {
		browser: match[ 1 ] || "",
		version: match[ 2 ] || "0"
	};
};

matched = jQuery.uaMatch( navigator.userAgent );
browser = {};

if ( matched.browser ) {
	browser[ matched.browser ] = true;
	browser.version = matched.version;
}

// Chrome is Webkit, but Webkit is also Safari.
if ( browser.chrome ) {
	browser.webkit = true;
} else if ( browser.webkit ) {
	browser.safari = true;
}

jQuery.browser = browser;

Solution 5 - Jquery

I've used the following code when I hit the same issue:

<script type="text/javascript">
 $(document).ready(function() {
    //if (!$.browser.webkit && ! $.browser.mozilla) { //depricated
    if (!navigator.userAgent.match(/mozilla/i) && 
        ! navigator.userAgent.match(/webkit/i) ){
        alert('Let me tell you about Mozilla');
    }
 });
</script>

Solution 6 - Jquery

You could just not update until you move away from depreciated methods.

You really shouldn't be including jquery from a CDN without specifying a version number anyway, it in a way defeats the purpose of using a CDN (no cacheing).

Here's a link to the latest version of jQuery that supported $.browser:

http://code.jquery.com/jquery-1.8.3.min.js

simply replace your jquery.js src with that link and your code will continue to run until you are ready to move forward and stop using depreciated features.

Note: Fancybox2 still uses $.browser, that's the most common one I've seen so far since the update.

Update: Slickgrid is still using $.browser, no update as of 02/11/2013

Solution 7 - Jquery

I abstracted the logic from jQuery.browser into a plugin jquery.browser. The plugin is released under the MIT license.

I've also added support for IE11, Opera Webkit and Android detection.

Solution 8 - Jquery

Try Conditionizr

Hope this Helps :)

Solution 9 - Jquery

Exact version of IE can be detected by additional checking of existence of standard global objects added in specific IE versions.

10 or older	document.all
9 or older	document.all && !window.atob
8 or older	document.all && !document.addEventListener
7 or older	document.all && !document.querySelector
6 or older	document.all && !window.XMLHttpRequest
5.x	document.all && !document.compatMode

if (document.all && !document.querySelector) {
    alert('IE7 or lower');
}

These tests avoid using the userAgent which is used for spoofing

Solution 10 - Jquery

if(!$.browser){
    $.browser={chrome:false,mozilla:false,opera:false,msie:false,safari:false};
    var ua=navigator.userAgent;
        $.each($.browser,function(c,a){
        $.browser[c]=((new RegExp(c,'i').test(ua)))?true:false;
            if($.browser.mozilla && c =='mozilla'){$.browser.mozilla=((new RegExp('firefox','i').test(ua)))?true:false;};
            if($.browser.chrome && c =='safari'){$.browser.safari=false;};
        });
};

http://jsfiddle.net/R3AAX/3/

Solution 11 - Jquery

If all you want is your 3rd party jQuery plugins to be able to use jQuery.browser.msie, here's a one-liner. Just include it after jQuery.

jQuery.browser = jQuery.browser || {msie: navigator.userAgent.match(/msie/i) ? true : false};

This is the dumbest-possible fix, but it's all I needed, and it does work, so here you go!

Solution 12 - Jquery

To add to this discussion. I just came up with a jquery $.browser plugin that, instead of 'detection', just merely parses the user agent into an easy-to-use object. Further logic can easily be applied to further break down and parse specific browsers and platforms.

I have had very reliable results on useragents found here: UserAgentString.com. I even included the version detection for ie11 (near the bottom).

//The following code is by no means perfect, nor is it meant to be a standalone 'detection' plugin. 
//It demonstrates parsing the useragent string into an easy to manage object. 
//Even if it does make detection rediculously easy.. :)

//Because this regex makes no assumptions in advance.
//IMO, It's compatibilty and maintainability is much higher than those based on static identifiers.

/*
  uaMatch replacement that parses a useragent string into an object
  useragent segments can be Name Value OR Name/Value OR Name

  Segment: Name Value
    Name: parsed to the last whitespace character
    Value: value after the last whitespace character
    Matches: (.NET CLR) (#.##), Android 2.3.4, etc
    Note: this regex can have leading/trailing whitespace (trimmed for object properties)
  
  Segment: Name/Value
    Matches: all values matching Name/Value
    Example: Firefox/24.0, Safari/533.1, Version/12.1, etc
  
  Segment: Name
    Matches: identifiers that hold no values (value of 'true' is implied)
    Example: Macintosh, Linux, Windows, KHTML, U, etc
  
  
   WARNING: not necessarily compatible with jQuery's $.browser implementation.
   - not recommended as a replacement for plugins that require it to function.
*/
(function ($) {

    var ua = navigator.userAgent.toLowerCase();

    var regex = /compatible; ([\w.+]+)[ \/]([\w.+]*)|([\w .+]+)[: \/]([\w.+]+)|([\w.+]+)/g;
    var match = regex.exec(ua);

    var browser = { };

    while (match != null) {
        var prop = {};

        if (match[1]) {
          prop.type = match[1];
          prop.version = match[2];
        }
        else if (match[3]) {
          prop.type = match[3];
          prop.version = match[4];
        }
        else {
          prop.type = match[5];
        }

        // some expressions have leading whitespace (i couldn't avoid this without a more complex expression)
        // trim them and get rid of '.' (' .NET CLR' = 'net_clr') 
        prop.type = $.trim(prop.type).replace(".","").replace(" ","_"); 
        var value = prop.version ? prop.version : true;
        
        if (browser[prop.type]) {
            if (!$.isArray(browser[prop.type]))
                browser[prop.type] = new Array(browser[prop.type]);

            browser[prop.type].push(value);
        }    
        else browser[prop.type] = value;
        
        match = regex.exec(ua);
    }
    
    for (var i in browser)
        if (i.indexOf("mac") > -1)
            browser.mac = true;
    
    if (browser.windows_nt && !browser.windows)
        browser.windows = true;
        
    //put known browsers into the 'version' property for 'some' jquery compatibility
    
    //for sake of argument chromium 'is' chrome
    if (browser.chromium && !browser.chrome)
        browser.chrome = browser.chromium;

    //chrome / safari / webkit
    if (browser.chrome) {
        browser.version = browser.chrome;
    }
    else if (browser.safari) {
        browser.version = browser.safari;
    }
    else {
        if (browser.applewebkit)
            browser.webkit = browser.applewebkit;
            
        if (browser.webkit)
            browser.version = browser.webkit;
    }
    
    //firefox / gecko
    if (browser.firefox) {
        if (browser.rv)
            browser.version = browser.rv;
            
        else browser.version = browser.firefox;
    }
    else if (browser.gecko) {
        if (browser.rv)
            browser.version = browser.rv;
            
        else browser.version = browser.gecko;
    }
    
    //opera
    if (browser.opera && !browser.version)
        browser.version = browser.opera;
    
    //msie
    if (browser.trident && browser.rv) //ie11
        browser.msie = browser.rv;

    if (browser.msie)
        browser.version = browser.msie;
  
    $.browser = browser;//Rename to reduce confliction?
    
    //WAS USED FOR TESTING & DISCOVERY (very useful)
    //TODO: remove line below
    alert(JSON.stringify($.browser));
  
}) (jQuery);

On Internet Explorer 10, JSON.stringify will ouput something like this:

{"mozilla":"5.0","msie":"10.0","windows_nt":"6.2","trident":"6.0","net4.0e":true,"net4.0c":true,"net_clr":["3.5.30729","2.0.50727","3.0.30729"],"windows":true,"version":"10.0"}

Solution 13 - Jquery

Short and yet Powerful.

// chrome, safari, webkit, mozilla, msie, opera
var chrome = /chrome/i.test(navigator.userAgent); 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjchwebdevView Question on Stackoverflow
Solution 1 - JqueryFedeView Answer on Stackoverflow
Solution 2 - JqueryBradView Answer on Stackoverflow
Solution 3 - Jqueryuser989952View Answer on Stackoverflow
Solution 4 - JquerySynthyView Answer on Stackoverflow
Solution 5 - JqueryAlexx RocheView Answer on Stackoverflow
Solution 6 - JqueryKevin BView Answer on Stackoverflow
Solution 7 - JqueryGabriel CebrianView Answer on Stackoverflow
Solution 8 - JqueryIjas AmeenudeenView Answer on Stackoverflow
Solution 9 - JqueryVincent GloaguenView Answer on Stackoverflow
Solution 10 - JqueryetbView Answer on Stackoverflow
Solution 11 - JqueryBen HullView Answer on Stackoverflow
Solution 12 - JquerySilverXView Answer on Stackoverflow
Solution 13 - JqueryVictorView Answer on Stackoverflow