怎么用js判断是pc端还是手机端

JavaScript015

怎么用js判断是pc端还是手机端,第1张

<%-- 手机端电脑端判断--%>

<script type="text/javascript">

$(document).ready(function () {

//平台、设备和操作系统

var system = {

win: false,

mac: false,

xll: false

}

//检测平台

var p = navigator.platform

system.win = p.indexOf("Win") == 0

system.mac = p.indexOf("Mac") == 0

system.x11 = (p == "X11") || (p.indexOf("Linux") == 0)

//跳转语句

if (system.win || system.mac || system.xll) {//转向后台登陆页面

window.location.href = "index.html"

} else {

window.location.href = "HomeIndex.aspx"//手机

}

})

</script>

应该把JS 放在页面首页加载 就行

function IsPC() {

var userAgentInfo = navigator.userAgent

var Agents = ["Android", "iPhone",

"SymbianOS", "Windows Phone",

"iPad", "iPod"]

var flag = true

for (var v = 0v <Agents.lengthv++) {

if (userAgentInfo.indexOf(Agents[v]) >0) {

flag = false

break

}

}

return flag

}

var flag = IsPC()//true为PC端,false为手机端

alert(flag)

var bForcepc=fGetQuery("dv")=="pc"

function fBrowserRedirect(){

var sUserAgent=navigator.userAgent.toLowerCase()

var bIsIpad=sUserAgent.match(/ipad/i)=="ipad"

var bIsIphoneOs=sUserAgent.match(/iphone os/i)=="iphone os"

var bIsMidp=sUserAgent.match(/midp/i)=="midp"

var bIsUc7=sUserAgent.match(/rv:1.2.3.4/i)=="rv:1.2.3.4"

var bIsUc=sUserAgent.match(/ucweb/i)=="ucweb"

var bIsAndroid=sUserAgent.match(/android/i)=="android"

var bIsCE=sUserAgent.match(/windows ce/i)=="windows ce"

var bIsWM=sUserAgent.match(/windows mobile/i)=="windows mobile"

if(bIsIpad||bIsIphoneOs||bIsAndroid||bIsMidp||bIsUc7||bIsUc||bIsCE||bIsWM

{

    alert("手机客户端")

         if(!bForcepc){

             alert("手机客户端")

         }

}

}

function fGetQuery(name){

var sUrl=window.location.search.substr(1)

var r = sUrl.match(new RegExp("(^|&)"+name+"=([^&]*)(&|$)"))

return (r==null ? null : unescape(r[2]))

}