首页 > 其他分享 >判断系统的方法

判断系统的方法

时间:2023-03-27 19:33:16浏览次数:34  
标签:判断 方法 系统 test iphone var userAgentInfo android platformName


 

公司的项目需要对通过浏览器方法的设备进行检测,是pad,iphone,windows,android,linux等,

因此只能通过浏览器的JS代码进行判断了,网上找到一篇比较好的文章,见下连接。

 

http://blog.sina.com.cn/s/blog_44d4a64b01014i9u.html

 

加上正则表达式,有个demo的代码,见下:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
		<script type="text/javascript">
		
			var userAgentInfo = navigator.userAgent.toLowerCase();
			 alert(userAgentInfo) ;
			var reWindow = /windows/; 
			var reAndroid = /android/; 
			var reIphone = /iphone/;
			var reIpad = /ipad/;
			
		    var platformName ;
		    
		    if (reWindow.test(userAgentInfo)) {
		    	platformName = "windows" ;
		    }else if (reIphone.test(userAgentInfo)) {
		    	platformName = "iphone" ;
		    }else if (reIpad.test(userAgentInfo)) {
		    	platformName = "ipad" ;
		    }else if (reAndroid.test(userAgentInfo)) {
		    	platformName = "android" ;
		    }
		    
		    alert(platformName) ;
		</script>
	</head>
	<body>

	</body>
</html>

  

 从 window7, iphone5, ipad设备检测,正确。浏览器分布是火狐,safari,safari。

 

标签:判断,方法,系统,test,iphone,var,userAgentInfo,android,platformName
From: https://blog.51cto.com/u_16034393/6152976

相关文章