Multiple solutions to the problem that $. browser. msie is null or not an object

  • 2021-08-05 08:15:23
  • OfStack

'$. browser. msie' is empty or not an object, this is an jQuery error
This error occurred because the jQuery version was upgraded from before 1.9 to after 1.9, and an error was reported because $. browser. msie did not exist in jQuery after 1.9.

jQuery 1.9 Remove $. browser Alternative Method
$. browser is a regular expression that matches userAgent to determine the version and type of browser. jquery. browser and jquery. browser. version have been stated in the jquery 1.3. 2 documentation that they are recommended to be discarded and can be replaced by jquery. support.

jQuery started with version 1.9, removing $. browser and $. browser. version and replacing it with $. support. In the newer version 2.0, IE 6/7/8 will no longer be supported. In the future, if users need to support IE 6/7/8, they can only use jQuery 1.9 or jQuery 1.10. 1, etc. If you want to fully support IE and use a mixture of jQuery 1.9 and 2.0, the official solution is:

Solution 1:


<!--[if lt IE 9]>
<script src='/jquery-1.10.1.min.js'></script>
<![endif]-->
<!--[if gte IE 9]>
<script src='/jquery-2.0.2.min.js'></script>
<![endif]-->

Solution 2:

Just don't use $. browser. msie to judge, and find some solutions that can be directly replaced on the Internet


$.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());
$.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());
$.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());
$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());

The expression after the equal sign returns true/false, which can be directly used to replace the original $. browser. msie and so on.

Check for IE6:


// Old
if ($.browser.msie && 7 > $.browser.version) {}
// New
if ('undefined' == typeof(document.body.style.maxHeight)) {}

Check for IE 6-8:

$. support. leadingWhitespace is a unique property in IE, so you can use $. support. leadingWhitespace to determine whether the browser is IE6-8

Code 1


if (!$.support.leadingWhitespace) {}

Code 2


$(function($){
  var ieFlag= $.support.leadingWhitespace;// Definition judgment IE8 Variables of 
  if(!ieFlag){//IE8 The following 
   //IE Code 
  }else{
   // Other codes 
  }
 });

Solution 3

/* After the discovery of judging the browser type, the general type */an article written by a foreigner can be tested under IE, Firefox and Google

Original address: http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-

-

-

-and-and-opera-browser


// Firefox 1.0+
 var isFirefox = typeof InstallTrigger !== 'undefined';
 alert("isFirefox:"+isFirefox);
 // Opera 8.0+
 var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
 alert("isOpera:"+isOpera);
 // Safari <= 9 "[object HTMLElementConstructor]"
 var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
 alert("isSafari:"+isSafari);
 // Internet Explorer 6-11
 var isIE = /*@cc_on!@*/ false || !!document.documentMode;
 alert("isIE:"+isIE);
 // Edge 20+
 var isEdge = !isIE && !!window.StyleMedia;
 alert("isEdge:"+isEdge);
 // Chrome 1+
 var isChrome = !!window.chrome && !!window.chrome.webstore;
 alert("isChrome:"+isChrome);
 // Blink engine detection(7)
 var isBlink = (isChrome || isOpera) && !!window.CSS;
 alert("isBlink:"+isBlink);

Solution 4:

Recently, I upgraded the jQuery of a project to the latest version, and found that some pages reported the following errors

Cannot read property 'msie' of undefined

I searched the jQuery website for 1 time, because $. browser, the api, was officially abolished from jQuery 1.9, and this error will be reported as long as $. browser is used in js code. Please refer to jQuery official instructions for details.

By the way, the landlord expanded and read 1, and found that jQuery 1.9 officially deleted all api marked as deprecated in previous versions, and was no longer backward compatible at all. For kids who upgrade to the latest jQuery, this means spending 1 extra time upgrading the code to use the new api, or implementing these deleted methods yourself. Fortunately, the jQuery team realized the inconvenience caused by this break change, and launched a plug-in called jQuery Migrate, which can automatically recover those API that were discarded in the latest version. As long as this plug-in is referenced, the existing js code can run normally with the latest jQuery library 1 without changing it.

The following is a concrete solution. First, download the jQuery Migrate plug-in, and then add a reference to the jQuery Migrate js file after the reference to jQuery js.


<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>

The solution with jQuery Migrate is simple and mindless, but it will lead to the addition of an additional js library. For kids who don't like to add extra js libraries, please note that the following code is loaded after the jQuery file and before the code of $. browser.


jQuery.browser={};(function(){jQuery.browser.msie=false; jQuery.browser.version=0;if(navigator.userAgent.match(/MSIE ([0-9]+)./)){ jQuery.browser.msie=true;jQuery.browser.version=RegExp.$1;}})();

Above is this site this site collates the solution, everybody can choose comparatively simple according to the project need.


Related articles: