• 保存到桌面  加入收藏  设为首页
HTML5

js-判断当前浏览器环境-移动端-PC端-微信

时间:2017-09-19 16:53:13   作者:江节胜   来源:胜行天下   阅读:779   评论:0
<script type="text/javascript">
		function isMobile1() {
			var sUserAgent = navigator.userAgent.toLowerCase();
			var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
			var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
			var bIsMidp = sUserAgent.match(/midp/i) == "midp";
			var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
			var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
			var bIsAndroid = sUserAgent.match(/android/i) == "android";
			var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
			var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

			if(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
				return true;
			} else {
				return false;
			}
		}

		function isMobile2() {
			var userAgentInfo = navigator.userAgent;
			var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPod"];
			var flag = true;
			for(var v = 0; v < Agents.length; v++) {
				if(userAgentInfo.indexOf(Agents[v]) > 0) {
					flag = true;
					break;
				}
			}
			if(window.screen.width >= 768) {
				flag = false;
			}
			return flag;
		}

		function isWeChat() {
			var ua = navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i) == "micromessenger") {
				return true;
			} else {
				return false;
			}
		}


		alert('当前' + (isMobile1() == true ? '是' : "不是") + "移动设备访问(isMobile1())");
		alert('当前' + (isMobile2() == true ? '是' : "不是") + "移动设备访问(isMobile2():增加 屏宽大于768直接为pc)");
		alert('当前' + (isWeChat() == true ? '是' : "不是") + "微信访问");
</script>

有任何疑问或技术合作都可联系我

微信:767000122 【推荐】

QQ:596957738


标签:js  浏览器环境  移动端  C端  微信  
相关评论

加我微信 596957738 (QQ同号)加我微信   QQ联系:596957738   地址:江苏省南京市雨花台区

苏公网安备32011402010305号

皖ICP备16019989号

江节胜的Gitee,江节胜的Git地址