js判断手机浏览器操作系统和微信浏览器的方法

网络编程 发布日期:2024/10/4 浏览次数:1

正在浏览:js判断手机浏览器操作系统和微信浏览器的方法

今天就为大家介绍一下用js判断手机客户端平台及系统平台的方法:

<script type="text/javascript">
//手机端判断各个平台浏览器及操作系统平台
function checkPlatform(){
	if(/android/i.test(navigator.userAgent)){
		document.write("This is Android'browser.");//这是Android平台下浏览器
	}
	if(/(iPhoneiPadiPodiOS)/i.test(navigator.userAgent)){
		document.write("This is iOS'browser.");//这是iOS平台下浏览器
	}
	if(/Linux/i.test(navigator.userAgent)){
		document.write("This is Linux'browser.");//这是Linux平台下浏览器
	}
	if(/Linux/i.test(navigator.platform)){
		document.write("This is Linux operating system.");//这是Linux操作系统平台
	}
	if(/MicroMessenger/i.test(navigator.userAgent)){
		document.write("This is MicroMessenger'browser.");//这是微信平台下浏览器
	}
}
 $(document).ready(function(){
  alert(navigator.platform);
  checkPlatform();
 });
</script>

注意上面的代码使用了jquery。需要加载jquery才可以运行。当然你可以把触发函数给删除了

在这里就不做pc端操作系统平台的判断了,怎么判断也说下方法,如用document.write(navigator.platform);就可以获得操作系统平台。在win32下就会显示Win32,;在Win64下就会显示Win64等。

下面小编为大家补充几个不错的函数

<script type="text/javascript">
var browser = {
	versions : function () {
		var u = navigator.userAgent,
		app = navigator.appVersion;
		return {
			trident : u.indexOf('Trident') > -1,
			presto : u.indexOf('Presto') > -1,
			webKit : u.indexOf('AppleWebKit') > -1,
			gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,
			mobile : !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/),
			ios : !!u.match(/\(i[^;]+;( U;)"qq浏览器");
}else
{
document.write("其它浏览器");
}
</script>

亲测,非常好用。