js获取浏览器类型进行判断

本文为webuploader.js中学习心得,感谢开源,从中加入了ie的edge判断
/**
		 * @description  简单的浏览器检查结果。
		 *
		 * * `webkit`  webkit版本号,如果浏览器为非webkit内核,此属性为`undefined`。
		 * * `chrome`  chrome浏览器版本号,如果浏览器为chrome,此属性为`undefined`。
		 * * `ie`  ie浏览器版本号,如果浏览器为非ie,此属性为`undefined`。
		 * * `firefox`  firefox浏览器版本号,如果浏览器为非firefox,此属性为`undefined`。
		 * * `safari`  safari浏览器版本号,如果浏览器为非safari,此属性为`undefined`。
		 * * `opera`  opera浏览器版本号,如果浏览器为非opera,此属性为`undefined`。
		 *
		 * @property {Object} [browser]
		 */
		$scope.browser = function() {
			var ua = navigator.userAgent;
			var ret = {}, webkit = ua.match(/WebKit\/([\d.]+)/), chrome = ua
					.match(/Chrome\/([\d.]+)/)
					|| ua.match(/CriOS\/([\d.]+)/),

			ie = ua.match(/MSIE\s([\d\.]+)/) || ua.match(/Edge\/([\d.]+)/)
					|| ua.match(/(?:trident)(?:.*rv:([\w.]+))?/i), firefox = ua
					.match(/Firefox\/([\d.]+)/), safari = ua
					.match(/Safari\/([\d.]+)/), opera = ua
					.match(/OPR\/([\d.]+)/);

			webkit && (ret.webkit = parseFloat(webkit[1]));
			chrome && (ret.chrome = parseFloat(chrome[1]));
			ie && (ret.ie = parseFloat(ie[1]));
			firefox && (ret.firefox = parseFloat(firefox[1]));
			safari && (ret.safari = parseFloat(safari[1]));
			opera && (ret.opera = parseFloat(opera[1]));

			return ret;
		};
		if ($scope.browser().ie) {
			alert("我是ie");
		}

猜你喜欢

转载自1197581932.iteye.com/blog/2400129