检测浏览器版本并升级jQuery插件

前言

由于项目使用的是angular.js1.4,但从1.2开始angular对IE9如下的内核再也不支持了。而国内还有须要用户使用的是360和firfox浏览器。我就在找一个提醒用户进行更新的方法。javascript

这是一种解决方式,可是在angular环境中不能使用,自定义建立的弹出层没法append到body里。由于angular执行时,body尚未建立。而后代码报错。页面就中止加载。因此,若是要解决这个问题,就须要在body加载前处理。css

我使用的方式是,检测浏览器版本,系统版本若是过旧的话。直接跳转到新页面,在新页面下载浏览器。html

demo

<linkhref="~/jReject/css/jquery.reject.css"rel="stylesheet"/>
<scriptsrc="~/jReject/js/jquery.reject.js"></script>
<scriptsrc="jquery-1.9.1.min.js"></script>
<scripttype="text/javascript">
        $(function() {
            needDownloadNewExplorer();
        });
        function needDownloadNewExplorer() {
            setTimeout(function () {
                $.reject({
                    reject: {
                        safari: true, // Apple Safari
                        chrome: true, // Google Chrome
                        firefox: true, // Mozilla Firefox
                        msie: true, // Microsoft Internet Explorer
                        opera: true, // Opera
                        konqueror: true, // Konqueror (Linux)
                        unknown: true // Everything else
                    },
                    imagePath: './jReject/images/',
                    browserInfo: { // Settings for which browsers to display
                        chrome: {
                            // Text below the icon
                            text: 'Google Chrome',
                            // URL For icon/text link
                            url: 'http://rj.baidu.com/soft/detail/14744.html',
                            // (Optional) Use "allow" to customized when to show this option
                            // Example: to show chrome only for IE users
                            // allow: { all: false, msie: true }
                        },
                        firefox: {
                            text: 'Mozilla Firefox',
                            url: 'http://rj.baidu.com/soft/detail/11843.html'
                        },
                        safari: {
                            text: 'Safari',
                            url: 'http://www.apple.com/safari/download/'
                        },
                        opera: {
                            text: 'Opera',
                            url: 'http://www.opera.com/download/'
                        },
                        msie: {
                            text: 'Internet Explorer',
                            url: 'http://www.microsoft.com/windows/Internet-explorer/'
                        }
                    },
                    closeLink: '关闭此窗口',
                    header: '若是本网页显示有问题,请选择下载以下浏览器的最新版本', // Header Text
                    paragraph1: '', // Paragraph 1
                    paragraph2: '',
                    closeMessage: '' // Message below close window link
                }); // Customized Browsers
            }, 2000);
        }
</script>

执行的结果以下java

注意修改图片存放路径jquery

这个插件须要用到jQuery,jQuery的版本要2.0如下的。由于2.0以上的版本再也不支持IE9如下。git

这个插件的github地址为 https://github.com/TurnWheel/jRejectgithub

页面的demo地址为 http://jreject.turnwheel.com/chrome

我使用的方式

window.location.href="http://baidu.com"//跳转新页面

下面这个是在360网站找到的check浏览器内核和操做系统版本的代码windows

http://se.360.cn/v5/laboratory6.htm //这个是360检测浏览器内核的网站。能够直接查看源代码以下:浏览器

<script>
		var bs = "<span>浏览器内核:</span><span class='big'>非IE内核</span>";
		user_agent = navigator.userAgent.toLowerCase();

		if (user_agent.indexOf("msie 10.0")>-1&&user_agent.indexOf("trident/6.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE10</span>";
		}else if (user_agent.indexOf("msie 9.0")>-1&&user_agent.indexOf("trident/6.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE10(兼容模式)</span>";
		}else if (user_agent.indexOf("msie 8.0")>-1&&user_agent.indexOf("trident/6.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE10(兼容模式)</span>";
		}else if (user_agent.indexOf("msie 7.0")>-1&&user_agent.indexOf("trident/6.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE10(兼容模式)</span>";
		}else if(user_agent.indexOf("msie 9.0")>-1) {
			bs = "<span>浏览器内核:</span><span class='big'>IE9</span>";
		}else if (user_agent.indexOf("msie 7.0")>-1&&user_agent.indexOf("trident/5.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE9(兼容模式)</span>";
		}else if (user_agent.indexOf("msie 8.0")>-1&&user_agent.indexOf("trident/5.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE9(兼容模式)</span>";
		}else if(user_agent.indexOf("msie 8.0")>-1) {
			bs = "<span>浏览器内核:</span><span class='big'>IE8</span>";
		}else if(user_agent.indexOf("msie 7.0")>-1&&user_agent.indexOf("trident/4.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE8(兼容模式)</span>";
		}else if(user_agent.indexOf("msie 7.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE7</span>";
		}else if(user_agent.indexOf("msie 6.0")>-1){
			bs = "<span>浏览器内核:</span><span class='big'>IE6</span>";
		}
		
		var os={
			'5.0':'Windows 2000',
			'5.2':'Windows 2003',
			'5.1':'Windows Xp',
			'6.0':'Windows Vista',
			'6.1':'Windows 7',
			'6.2':'Windows 8'
		};
		if(user_agent.match(/windows\s*nt\s*([0-9.]+)/))
		{
			if(os[RegExp.$1])
			{
			   os = os[RegExp.$1];
			}
		}else{
			os = window.navigator.platform;
		}
		document.getElementById('browser').innerHTML = bs+ "<br/><span>操做系统:</span><span class='big'>"+os+"</span>";
	</script>
相关文章
相关标签/搜索