I use the following snip of a script:
if ($.browser.msie && $.browser.version < 9) {
extra = "?" + Math.floor(Math.random() * 3000);
}
It works fine with jQuery 1.8.3.
Now I updated jQuery to the new version 1.9.1 to use a new script.
Now I get the following error:
TypeError: Cannot read property 'msie' of undefined
I read the change log of the new jQuery version, but nothing should have changed
with msie
Any known bugs, tips or proposals?
Since $.browser is deprecated, here is an alternative solution:
/**
* Returns the version of Internet Explorer or a -1
* (indicating the use of another browser).
*/
function getInternetExplorerVersion()
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
if ( ver >= 8.0 )
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert( msg );
}
However, the reason that its deprecated is because jQuery wants you to use feature detection instead.
An example:
$("p").html("This frame uses the W3C box model: <span>" +
jQuery.support.boxModel + "</span>");
And last but not least, the most reliable way to check IE versions:
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
// And to detect the version:
// ie === 6 // IE6
// ie > 7 // IE8, IE9 ...
// ie < 9 // Anything less than IE9
// ----------------------------------------------------------
// UPDATE: Now using Live NodeList idea from @jdalton
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : undef;
}());
You can detect the IE browser by this way.
(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
you can get reference on this URL: jquery.browser.msie Alternative
Update! Complete answer overhaul for new plugin!
The following plugin has been tested in all major browsers. It makes traditional use of userAgent
string to re-equip jQuery.browser
only if you're using jQuery version 1.9 or Greater!
It has the traditional jQuery.browser.msie
type properties as well as a few new ones, including a .mobile
property to help decide if user is on a mobile device.
Note: This is not a suitable replacement for feature testing. If you expect to support a specific feature on a specific device, it's still best to use traditional feature testing
/** jQuery.browser_x000D_
* @author J.D. McKinstry (2014)_x000D_
* @description Made to replicate older jQuery.browser command in jQuery versions 1.9+_x000D_
* @see http://jsfiddle.net/SpYk3/wsqfbe4s/_x000D_
*_x000D_
* @extends jQuery_x000D_
* @namespace jQuery.browser_x000D_
* @example jQuery.browser.browser == 'browserNameInLowerCase'_x000D_
* @example jQuery.browser.version_x000D_
* @example jQuery.browser.mobile @returns BOOLEAN_x000D_
* @example jQuery.browser['browserNameInLowerCase']_x000D_
* @example jQuery.browser.chrome @returns BOOLEAN_x000D_
* @example jQuery.browser.safari @returns BOOLEAN_x000D_
* @example jQuery.browser.opera @returns BOOLEAN_x000D_
* @example jQuery.browser.msie @returns BOOLEAN_x000D_
* @example jQuery.browser.mozilla @returns BOOLEAN_x000D_
* @example jQuery.browser.webkit @returns BOOLEAN_x000D_
* @example jQuery.browser.ua @returns navigator.userAgent String_x000D_
*/_x000D_
;;(function($){var a=$.fn.jquery.split("."),b;for(b in a)a[b]=parseInt(a[b]);if(!$.browser&&(1<a[0]||9<=a[1])){a={browser:void 0,version:void 0,mobile:!1};navigator&&navigator.userAgent&&(a.ua=navigator.userAgent,a.webkit=/WebKit/i.test(a.ua),a.browserArray="MSIE Chrome Opera Kindle Silk BlackBerry PlayBook Android Safari Mozilla Nokia".split(" "),/Sony[^ ]*/i.test(a.ua)?a.mobile="Sony":/RIM Tablet/i.test(a.ua)?a.mobile="RIM Tablet":/BlackBerry/i.test(a.ua)?a.mobile="BlackBerry":/iPhone/i.test(a.ua)?_x000D_
a.mobile="iPhone":/iPad/i.test(a.ua)?a.mobile="iPad":/iPod/i.test(a.ua)?a.mobile="iPod":/Opera Mini/i.test(a.ua)?a.mobile="Opera Mini":/IEMobile/i.test(a.ua)?a.mobile="IEMobile":/BB[0-9]{1,}; Touch/i.test(a.ua)?a.mobile="BlackBerry":/Nokia/i.test(a.ua)?a.mobile="Nokia":/Android/i.test(a.ua)&&(a.mobile="Android"),/MSIE|Trident/i.test(a.ua)?(a.browser="MSIE",a.version=/MSIE/i.test(navigator.userAgent)&&0<parseFloat(a.ua.split("MSIE")[1].match(/[0-9\.]{1,}/)[0])?parseFloat(a.ua.split("MSIE")[1].match(/[0-9\.]{1,}/)[0]):_x000D_
"Edge",/Trident/i.test(a.ua)&&/rv:([0-9]{1,}[\.0-9]{0,})/.test(a.ua)&&(a.version=parseFloat(a.ua.match(/rv:([0-9]{1,}[\.0-9]{0,})/)[1].match(/[0-9\.]{1,}/)[0]))):/Chrome/.test(a.ua)?(a.browser="Chrome",a.version=parseFloat(a.ua.split("Chrome/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Opera/.test(a.ua)?(a.browser="Opera",a.version=parseFloat(a.ua.split("Version/")[1].match(/[0-9\.]{1,}/)[0])):/Kindle|Silk|KFTT|KFOT|KFJWA|KFJWI|KFSOWI|KFTHWA|KFTHWI|KFAPWA|KFAPWI/i.test(a.ua)?(a.mobile="Kindle",_x000D_
/Silk/i.test(a.ua)?(a.browser="Silk",a.version=parseFloat(a.ua.split("Silk/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Kindle/i.test(a.ua)&&/Version/i.test(a.ua)&&(a.browser="Kindle",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0]))):/BlackBerry/.test(a.ua)?(a.browser="BlackBerry",a.version=parseFloat(a.ua.split("/")[1].match(/[0-9\.]{1,}/)[0])):/PlayBook/.test(a.ua)?(a.browser="PlayBook",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):_x000D_
/BB[0-9]{1,}; Touch/.test(a.ua)?(a.browser="Blackberry",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Android/.test(a.ua)?(a.browser="Android",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Safari/.test(a.ua)?(a.browser="Safari",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Firefox/.test(a.ua)?(a.browser="Mozilla",a.version=parseFloat(a.ua.split("Firefox/")[1].match(/[0-9\.]{1,}/)[0])):_x000D_
/Nokia/.test(a.ua)&&(a.browser="Nokia",a.version=parseFloat(a.ua.split("Browser")[1].match(/[0-9\.]{1,}/)[0])));if(a.browser)for(var c in a.browserArray)a[a.browserArray[c].toLowerCase()]=a.browser==a.browserArray[c];$.extend(!0,$.browser={},a)}})(jQuery);_x000D_
/* - - - - - - - - - - - - - - - - - - - */_x000D_
_x000D_
var b = $.browser;_x000D_
console.log($.browser); // see console, working example of jQuery Plugin_x000D_
console.log($.browser.chrome);_x000D_
_x000D_
for (var x in b) {_x000D_
if (x != 'init')_x000D_
$('<tr />').append(_x000D_
$('<th />', { text: x }),_x000D_
$('<td />', { text: b[x] })_x000D_
).appendTo($('table'));_x000D_
}
_x000D_
table { border-collapse: collapse; }_x000D_
th, td { border: 1px solid; padding: .25em .5em; vertical-align: top; }_x000D_
th { text-align: right; }_x000D_
_x000D_
textarea { height: 500px; width: 100%; }
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>_x000D_
<table></table>
_x000D_
I had this problem as well. We were using two versions of Jquery (1.11.3 and 1.8.3), one of which was causing this issue. I found a lightbox_me.js release that worked on both versions:
http://buckwilson.me/lightboxme/
It was just an easy replacement of an old file.
Using this:
if (navigator.userAgent.match("MSIE")) {}
Include jQuery migration plugin along with your jQuery library.
Instead of having the whole migration script added, you could simply add the following (extracted from the migration script)
$.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"
};
};
and then use it like so
$.uaMatch(navigator.userAgent)
The jQuery.browser options was deprecated earlier and removed in 1.9 release along with a lot of other deprecated items like .live.
For projects and external libraries which want to upgrade to 1.9 but still want to support these features jQuery have release a migration plugin for the time being.
If you need backward compatibility you can use migration plugin.
You can use :
var MSIE = jQuery.support.leadingWhitespace; // This property is not supported by ie 6-8
$(document).ready(function(){
if (MSIE){
if (navigator.vendor == 'Apple Computer, Inc.'){
// some code for this navigator
} else {
// some code for others browsers
}
} else {
// default code
}});
For simple IE detection I tend to use:
(/msie|trident/i).test(navigator.userAgent)
Visit the Microsoft Developer Network to learn about the IE useragent: http://msdn.microsoft.com/library/ms537503.aspx
Source: Stackoverflow.com