JS判断是手机访问还是PC端访问网站
2024-10-20 08:26:12
<script>
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i))) {
location.replace("http://m.helloweba.com")
}else{
document.write("请使用手机访问.");
}
</script>
百度的判断代码
复制代码 代码如下:
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
}
}
}
};
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
}
}
}
};
使用方法:
<SCRIPT type=text/javascript>uaredirect("手机站","WEB站");</SCRIPT>
最新文章
- CSS Sprites优缺点
- APM的飞行模式
- C++基础——模拟事务 (1)COMMAND模式
- SpringMVC学习(一)
- NAT123 解决80端口被封的问题
- 每日一九度之 题目1030:毕业bg
- Java 日期 Api
- Map迭代器
- MAC中在eclipse luna上搭建移动平台自己主动化測试框架(UIAutomator/Appium/Robotium/MonkeyRunner)关键点记录
- 【算法】超大数组去重(Java语言实现)
- c#高级语言编程(第一部分)
- 在centos6编译安装http-2.4
- xshell连接centos与ubuntu
- 细说addEventListener与事件捕获
- Spring MVC 使用介绍(六)—— 注解式控制器(二):请求映射与参数绑定
- Java读写文件,字符输入流FileReader 和 字符输出流FileWriter
- Python3求解字符串滤值与百元买百鸡算法
- HDU 1425 C++使用sort函数
- MongoDB学习笔记(11) --- 聚合
- Linux 客户端bind函数的使用
热门文章
- lucene 异常 Lock obtain timed out 解决方法
- TCP/IP ---互联网的地址
- php 实现 html转js
- ucosii任务切换OS_TASK_SW()
- C# Enum,Int,String,之间及bool与int之间的转换
- SpringCloud系列九:脱离Eureka使用Ribbon
- python加密包
- PLSQL快捷键设置
- 虚拟化笔记04.OpenFiler.install
- 探讨把一个元素从它所在的div 拖动到另一个div内的实现方法