Home > Web Front-end > JS Tutorial > Jquery extension uses $.browser method

Jquery extension uses $.browser method

巴扎黑
Release: 2017-07-08 10:00:53
Original
1885 people have browsed it

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());
Copy after login



# 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) {}
Copy after login

// New

<span style="white-space:pre">	</span>if (&#39;undefined&#39; == typeof(document.body.style.maxHeight)) {}
Copy after login

Check if it is IE 6-8:##

<span style="white-space:pre">	</span>if (!$.support.leadingWhitespace) {}
Copy after login

*** *************************************************** *********************

The idea we adopt below is to use jquery’s

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"
        };
}
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template