方法一
function IsPC(){
var userAgentInfo = navigator.userAgent
var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod")
var flag = true
for (var v = 0v
if(userAgentInfo.indexOf(Agents[v]) >0) { flag = falsebreak}
}
return flag
}
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
//alert(navigator.userAgent)
window.location.href ="iPhone.html"
} else if (/(Android)/i.test(navigator.userAgent)) {
//alert(navigator.userAgent)
window.location.href ="Android.html"
} else {
window.location.href ="pc.html"
}
方法二
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.*/), //是否为移动终端
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 , //是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') >-1, //是否iPad
webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
weixin: u.indexOf('MicroMessenger') >-1, //是否微信
qq: u.match(/\sQQ/i) == " qq" //是否QQ
}
}(),
language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
if(browser.versions.mobile || browser.versions.ios || browser.versions.android ||
browser.versions.iPhone || browser.versions.iPad){
window.location = "http://m.zhaizhainv.com"
}
方法三: 摘自im- qq
var os = function() {
var ua = navigator.userAgent,
isWindowsPhone = /(?:Windows Phone)/.test(ua),
isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
isAndroid = /(?:Android)/.test(ua),
isFireFox = /(?:Firefox)/.test(ua),
isChrome = /(?:Chrome|CriOS)/.test(ua),
isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid &&!/(?:Mobile)/.test(ua)) || (isFireFox &&/(?:Tablet)/.test(ua)),
isPhone = /(?:iPhone)/.test(ua) &&!isTablet,
isPc = !isPhone &&!isAndroid &&!isSymbian
return {
isTablet: isTablet,
isPhone: isPhone,
isAndroid : isAndroid,
isPc : isPc
}
}()
使用方法
if(os.isAndroid || os.isPhone){
alert("-----")
}
方法四:
if(/AppleWebKit.*mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
if(window.location.href.indexOf("?mobile")<0){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location.href="手机页面"
}elseif(/iPad/i.test(navigator.userAgent)){
window.location.href="平板页面"
}else{
window.location.href="其他移动端页面"
}
}catch(e){}
}
}
方法五
//平台、设备和操作系统
varsystem ={
win:false,
mac:false,
xll:false
}
//检测平台
varp = navigator.platform
system.win = p.indexOf("Win") ==0
system.mac = p.indexOf("Mac") ==0
system.x11 = (p =="X11") || (p.indexOf("Linux") ==0)
//跳转语句,如果是手机访问就自动跳转到caibaojian.com页面
if(system.win||system.mac||system.xll){
}else{
window.location.href="http://caibaojian.com"
}
方法六
functionis_mobile(){
varregex_match =/(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i
varu = navigator.userAgent
if(null== u) {
returntrue
}
varresult = regex_match.exec(u)
if(null== result) {
returnfalse
}else{
returntrue
}
}
if(is_mobile()) {
document.location.href='http://caibaojian.com'//修改http://caibaojian.com为你所需跳转目标页地址
}
方法7 百度webapp版
<!---识别手机或电脑的js开始--->
(function(){
varres = GetRequest()
varpar = res['index']
if(par!='gfan'){
varua=navigator.userAgent.toLowerCase()
varcontains=function(a, b){
if(a.indexOf(b)!=-1){returntrue}
}
//将下面的http://caibaojian.com改成你的wap手机版页面地址 如我的 http://caibaojian.com
vartoMobileVertion =function(){
window.location.href ='http://caibaojian.com/'
}
if(contains(ua,"ipad")||(contains(ua,"rv:1.2.3.4"))||(contains(ua,"0.0.0.0"))||(contains(ua,"8.0.552.237"))){returnfalse}
if((contains(ua,"android") &&contains(ua,"mobile"))||(contains(ua,"android") &&contains(ua,"mozilla")) ||(contains(ua,"android") &&contains(ua,"opera"))
||contains(ua,"ucweb7")||contains(ua,"iphone")){toMobileVertion()}
}
})()
functionGetRequest(){
varurl = location.search//获取url中"?"符后的字串
vartheRequest =newObject()
if(url.indexOf("?") !=-1) {
varstr = url.substr(1)
strs = str.split("&")
for(vari =0i <strs.lengthi ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1])
}
}
returntheRequest
}
/***判断是否是通过手机访问
*/
public static function isMobile() {
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
return true
}
//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset($_SERVER['HTTP_VIA'])) {
//找不到为flase,否则为true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false
}
//判断手机发送的客户端标志,兼容性有待提高
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp',
'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu',
'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi',
'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile')
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true
}
}
//协议法,因为有可能不准确,放到最后判断
if (isset($_SERVER['HTTP_ACCEPT'])) {
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return true
}
}
return false
}
<coding-1 lang="other"><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, //
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终端器
iPhone: u.indexOf('iPhone') >-1 || u.indexOf('Mac') >-1, //是否为iPhone
iPad: u.indexOf('iPad') >-1, //是否iPad
webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
}
}()
}
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>
</coding>
JavaScript不管是判断PC浏览器还是手机浏览器,都是通过User Agent 来判断。