js判断手机访问或者PC的几个例子(常用于手机跳转)
例子一
<script>
functionbrowserRedirect(){
varsUserAgent=navigator.userAgent.toLowerCase();
varbIsIpad=sUserAgent.match(/ipad/i)=="ipad";
varbIsIphoneOs=sUserAgent.match(/iphoneos/i)=="iphoneos";
varbIsMidp=sUserAgent.match(/midp/i)=="midp";
varbIsUc7=sUserAgent.match(/rv:1.2.3.4/i)=="rv:1.2.3.4";
varbIsUc=sUserAgent.match(/ucweb/i)=="ucweb";
varbIsAndroid=sUserAgent.match(/android/i)=="android";
varbIsCE=sUserAgent.match(/windowsce/i)=="windowsce";
varbIsWM=sUserAgent.match(/windowsmobile/i)=="windowsmobile";
if(!(bIsIpad||bIsIphoneOs||bIsMidp||bIsUc7||bIsUc||bIsAndroid||bIsCE||bIsWM)){
window.location.href='https://www.nhooo.com';
}else{
//window.location.href='http://m.jb51.net';
}
}
$(function(){
browserRedirect();
});
</script>
例子2
<scripttype="text/javascript">
(function(){
varreWriteUrl=function(url){
if(url){
varSplits=url.split("/"),siteName=window.location.pathname;
if(typeofsiteName!=="undefined"){
return"http://m.jb51.net"+siteName;
}
}
};
if(/Android|webOS|iPhone|iPad|WindowsPhone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){
varurl=window.location.href;
varpathname=window.location.pathname;
if(url.indexOf("?mobile")<0){
try{
window.location.href=reWriteUrl(url);
}catch(e){}
}
};
})();
</script>
注意,如果给搜索引擎看的话我们在文件头加入
<metahttp-equiv="mobile-agent"content="format=xhtml;url=http://m.jb51.net"/>
<metahttp-equiv="mobile-agent"content="format=html5;url=http://m.jb51.net"/>
意思是告诉搜索引擎移动蜘蛛抓取的是手机版本的.