通过js语句判断web网站的访问端是电脑还是手机
展开全部
思路:对平台进行判断,使用js的window.navigator.platform。
例子(针对windows平台):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>得到浏览器信息</title>
</head>
<body>
<div id="div" style="width: 100px;height: 100px;"></div>
<script>
var version=window.navigator.platform;
if (version.toString()=="Win32")
{
document.getElementById("div").innerHTML="你使用的是Windows平台";
}
else
{
document.getElementById("div").innerHTML="你使用的不是Windows平台";
}
</script>
</body>
</html>
也可以加入Linux平台和Mac平台的一些判断,例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>得到浏览器信息</title>
</head>
<body>
<div id="div" style="width: 100px;height: 100px;"></div>
<script>
var version=window.navigator.platform;
if (version=="Win32" || version=="MacPPC" || version=="Linuxi586")
{
document.getElementById("div").innerHTML="电脑";
}
else
{
document.getElementById("div").innerHTML="手机";
}
</script>
</body>
</html>
如上代码所得效果图:
展开全部
可以利用js的navigator.userAgent属性进行判断。
判断是安卓手机的代码如下:
var u = navigator.userAgent;
if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {alert("是安卓手机")}
判断是iPhone手机的代码如下:
var u = navigator.userAgent;
if (u.indexOf('iPhone') > -1) {alert("是苹果手机")}
排除这两种情况,剩下的就是电脑端了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-05-29
展开全部
四种方法
1.
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
2.
function browserRedirect() {
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 || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
window.location.href=B页面;
}
}
3.正则匹配
var browser_class = navigator.userAgent;
var browser_class_name1 = browser_class.match("Mobile");
var browser_class_name2 = browser_class.match("mobile");
var location_url = window.location.href;
if (browser_class_name1 != null || browser_class_name2 != null) {
if (location_url.match("wap") == null) {
window.location.href = "
}
} else {
if (location_url.match("3g") != null || location_url.match("wap") != null) {
window.location.href = "
}
}
4.百度判断
function uaredirect(f) {
try {
if (document.getElementById("bdmark") != null) {
return
}
var b = false;
if (arguments[1]) {
var e = window.location.host;
var a = window.location.href;
if (isSubdomain(arguments[1], e) == 1) {
f = f + "/#m/" + a;
b = true
} else {
if (isSubdomain(arguments[1], e) == 2) {
f = f + "/#m/" + a;
b = true
} else {
f = a;
b = false
}
}
} else {
b = true
}
if (b) {
var c = window.location.hash;
if (!c.match("fromapp")) {
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS)/i))) {
location.replace(f)
}
}
}
} catch(d) {}
}
function isSubdomain(c, d) {
this.getdomain = function(f) {
var e = f.indexOf("://");
if (e > 0) {
var h = f.substr(e + 3)
} else {
var h = f
}
var g = /^www\./;
if (g.test(h)) {
h = h.substr(4)
}
return h
};
if (c == d) {
return 1
} else {
var c = this.getdomain(c);
var b = this.getdomain(d);
if (c == b) {
return 1
} else {
c = c.replace(".", "\\.");
var a = new RegExp("\\." + c + "$");
if (b.match(a)) {
return 2
} else {
return 0
}
}
}
};
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询