Home >Web Front-end >JS Tutorial >Jquery extension uses $.browser method
Due to jquery 1.9.0 or above, jquery has removed support for $.browser and uses $.support to determine the browser type. As a result, many previous plug-ins reported errors
"Uncaught TypeError: Cannot read property 'msie' of undefined".
There are many online The solution is as follows:
Determine the browser type:
<span style="white-space:pre"> </span>$.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 ## returns true/false, which can be directly used to replace the original $.browser.msie, etc.
Check if it is IE6:
// Old
<span style="white-space:pre"> </span>if ($.browser.msie && 7 > $.browser.version) {}
// New<span style="white-space:pre"> </span>if ('undefined' == typeof(document.body.style.maxHeight)) {}
Check if it is IE 6-8:##<span style="white-space:pre"> </span>if (!$.support.leadingWhitespace) {}
*** *************************************************** *********************
inheritance
mechanism to jquery 1.11.1 version Expand it to support the $.browser method, which has achieved the purpose of compatibility with previous components.jQuery.extend({ browser: function() { var rwebkit = /(webkit)\/([\w.]+)/, ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/, rmsie = /(msie) ([\w.]+)/, rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/, browser = {}, ua = window.navigator.userAgent, browserMatch = uaMatch(ua); if (browserMatch.browser) { browser[browserMatch.browser] = true; browser.version = browserMatch.version; } return { browser: browser }; }, }); function uaMatch(ua) { ua = ua.toLowerCase(); var match = rwebkit.exec(ua) || ropera.exec(ua) || rmsie.exec(ua) || ua.indexOf("compatible") < 0 && rmozilla.exec(ua) || []; return { browser : match[1] || "", version : match[2] || "0" }; }Save the above code into jquery-browser.js and use it.
The above is the detailed content of Jquery extension uses $.browser method. For more information, please follow other related articles on the PHP Chinese website!