咱们能够经过userAgent来判断,好比检测某些关键字,例如:AppleWebKit*****Mobile或AppleWebKit,须要注意的 是有些浏览器的userAgent中并不包含AppleWebKit关键字,可能会是Linux,UCBrowser等等。javascript
下面是Javascript代码:
java
<script type="text/javascript"> /* * 智能机浏览器版本信息: */ var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return {//移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 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;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQ HD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部 }; }(), language:(navigator.browserLanguage || navigator.language).toLowerCase() } document.writeln("语言版本: "+browser.language); document.writeln(" 是否为移动终端: "+browser.versions.mobile); document.writeln(" ios终端: "+browser.versions.ios); document.writeln(" android终端: "+browser.versions.android); document.writeln(" 是否为iPhone: "+browser.versions.iPhone); document.writeln(" 是否iPad: "+browser.versions.iPad); document.writeln(navigator.userAgent); </script>
下面的jsp代码简单判断浏览器类型,若是是iphone或者android就跳转到手机版本网页:android
<% String ua = request.getHeader("User-Agent") ; if (ua != null) { if (ua.indexOf("iPhone") >-1 || ua.indexOf("iPad") >-1 || (ua.indexOf("ndroid") >-1 && ua.indexOf("WebKit") >-1)) { response.sendRedirect("http://localhost/mobile"); return ; } } %>
各移动浏览器的http user-agent:
1、微信的http user-agent(关键词MicroMessenger)
1. Iphone(苹果)平台微信的ucweb的useragent:
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2
2. Android(安卓)平台微信的ucweb的useragent:1
Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255
2、UC浏览器的http user-agent(关键词UCWEB)
1. android平台:
android平台ucweb急速模式开启下ucweb浏览器useragent:
UCWEB/2.0 (Linux; U; Adr 2.3; zh-CN; MI-ONEPlus) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile
android平台ucweb急速模式关闭状态下的http_user_agent:
Mozilla/5.0 (Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Mobile Safari/534.13
2. iPhone 平台
iPhone 平台极速模式开启状态下ucweb浏览器useragent:
UCWEB/2.0 (iOS; U; iPh OS 4_3_2; zh-CN; iPh4) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile
iPhone 平台极速模式关闭状态下 UA 示例以下:(OBUA 为自带浏览器 UA)\
OBUA UCBrowser/8.6.0.199 Mobile
3. iPad 平台
ipad平台下ucweb浏览器useragent:
Mozilla/5.0 (iPad; U; CPU OS 6_0 like Mac OS X; zh-CN; iPad2) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Safari/534.13
4. windows Phone平台
WP平台下ucweb浏览器useragent:
WP平台极速模式开启状态下:(以 Nokia 900 为例)
UCWEB/2.0 (Windows; U; wds7.10; zh-CN; Nokia 900) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile
5. 诺基亚平台
诺基亚手机Symbian&Java平台ucweb浏览器useragen:
Nokia 5800 XpressMusic/UCWEB8.9.0.253/50/999
安卓QQ浏览器HD版检测的结果是:mac, Safari,这个非常变态,本身看着处理吧
3个检测浏览器User-Agent信息的网站
3、一些特别的浏览器
QQ浏览器(android)
MQQBrowser/3.6/Adr (Linux; U; 4.0.3; zh-cn; HUAWEI U8818 Build/U8818V100R001C17B926;480*800)
魅族UC浏览器(android)
JUC (Linux; U; 2.3.5; zh-cn; MEIZU MX; 640*960) UCWEB8.5.1.179/145/33232
UC浏览器(iphone)
IUC(U;iOS 5.1;Zh-cn;320*480;)/UCWEB8.8.0.212/42/997
Opera mobile(android)
Opera/9.80 (Android 4.0.3; Linux; Opera Mobi/ADR-1210241554) Presto/2.11.355 Version/12.10
Opera mini(iphone)
Opera/9.80 (iPhone; Opera Mini/7.0.5/28.2690; U; zh) Presto/2.8.119 Version/11.10
三个在线网站,经过手机浏览器就能够在线检测
1. http://whatsmyuseragent.com/
2. http://whatsmyua.com/
3. http://www.useragentstring.com/
ios