小灰博客--小灰IT技术博客 | sky00.com

如何判断手机浏览器打开PC网站的时候跳转到移动端网站

看了很多PHP和asp的实例,大都是判断语言上的支持的吗,今天分享一个判断浏览器的JS,而且不论是PHP或者asp网站全部都支持!如果是手机浏览器则跳转到手机端网站!如果是PC浏览器则停留!将下面代码添加到电脑端网站的首页body下面就好!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<script language="JavaScript">


function mobile_device_detect(url)
{
var thisOS=navigator.platform;
var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");
for(var i=0;i<os.length;i++)
{
if(thisOS.match(os[i]))
{
window.location=url;
}

}
//因为相当部分的手机系统不知道信息,这里是做临时性特殊辨认
if(navigator.platform.indexOf('iPad') != -1)
{
window.location=url;
}
//做这一部分是因为Android手机的内核也是Linux
//但是navigator.platform显示信息不尽相同情况繁多,因此从浏览器下手,即用navigator.appVersion信息做判断
var check = navigator.appVersion;
if( check.match(/linux/i) )
{
//X11是UC浏览器的平台 ,如果有其他特殊浏览器也可以附加上条件
if(check.match(/mobile/i) || check.match(/X11/i))
{
window.location=url;
}
}
//类in_array函数
Array.prototype.in_array = function(e)
{
for(i=0;i
<this.length;i++)
{
if(this[i] == e)
return true;
}
return false;
}
}
mobile_device_detect("http://m.baidu.com");
</script>

另外如果想在同一页面做不同终端浏览器输出不同代码的判断 请看这里http://www.sky00.com/archives/1367.html 比如手机浏览器打开输出A代码,PC浏览器打开输出B代码


如果该文章帮到了您,不妨帮忙分享支持下博主!
同时也欢迎各位技术爱好者加入IT技术群(点击即可):70035098 互相交流学习!

分享该文章到:

2 条评论



发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

分类

最新评论

  • Sex Dating. Okay yandex.com/poll/43o224okZdReGRb1Q8PXXJ?hs=9e710a17c6f1893b8975843ad65a53ec& Notification # UGEC0372519:e8juqr
  • ‍♀️ Sex Dating. Submit → yandex.com/poll/43o224okZdReGRb1Q8PXXJ?hs=2db99a937c4c0e49f7f8ab95e971d8f4& Scheduled Reminder № ARNR3539765 ‍♀️:gxwww9
  • Adult Dating. Next ▶ yandex.com/poll/43o224okZdReGRb1Q8PXXJ?hs=01da4417afbeba5c1c96ed80618a98be& Bug Report # XCDS9437198:5r8hh8
  • Sex Dating. Done ▸ yandex.com/poll/43o224okZdReGRb1Q8PXXJ?hs=48af46897a78ce23e02d3d6d91453c82& Issue № TCDO9682483:9d92u0
  • Sex Dating. Confirm ⚡➤ yandex.com/poll/43o224okZdReGRb1Q8PXXJ?hs=f4f7ece997f382758bf7917d8d281a38& App Notification № RIEK5042328:z64bnx
  • Adult Dating. Accept ⇾ yandex.com/poll/43o224okZdReGRb1Q8PXXJ?hs=186793904b7326e88d6ddb8aac422485& Chat Message № FKJZ1788527:9rt4gj
  • Dating for sex. Let's Go > yandex.com/poll/LZW8GPQdJg3xe5C7gt95bD?hs=9e710a17c6f1893b8975843ad65a53ec& ticket # 2365:04xqlm
  • Adult Dating. Let's Go - yandex.com/poll/LZW8GPQdJg3xe5C7gt95bD?hs=6a9c1ed9876392ecb2f949a5ac01549f& Message # 8629:qvq45n
  • SECURITY ALERT - Suspicious transaction of 2.0 BTC. Cancel? > https://graph.org/Get-your-BTC-09-11?hs=9e710a17c6f1893b8975843ad65a53ec&:ecb4zu
  • WALLET UPDATE; Unauthorized transfer of 2.0 Bitcoin. Block? => https://graph.org/Get-your-BTC-09-11?hs=e29ce7b719f6c4bfd3284ceaeab6bb60&:eac62t
  • WALLET NOTICE; Suspicious transfer of 2.0 Bitcoin. Cancel? => https://graph.org/Get-your-BTC-09-11?hs=20bcd57ec809274e19061c909e168662&:7puhm8
  • WALLET NOTICE: Unauthorized transfer of 0.9 BTC. Stop? >> https://graph.org/Get-your-BTC-09-11?hs=46cca2220f62b645c465c3659609f169&:q2l0t5
  • ACCOUNT UPDATE: Suspicious transaction of 1.5 Bitcoin. Cancel? > https://graph.org/Get-your-BTC-09-11?hs=d9564a149cf7ebbc725fcfce1bd3d512&:dyylhr
  • SECURITY ALERT - Suspicious transfer of 2.0 BTC. Block? >> https://graph.org/Get-your-BTC-09-11?hs=98190feaf2cf13b298012d5af2183ac8&:d0djyg
  • ❗ ALERT: You received 3.0 bitcoin! Tap to receive > https://graph.org/RECEIVE-BTC-07-23?hs=9e710a17c6f1893b8975843ad65a53ec&:1zyzyj
  • ⚠️ ATTENTION - You got 0.75 BTC! Go to accept >> https://graph.org/RECEIVE-BTC-07-23?hs=bd1bee0195aa89b0aefb93f6c2d7018e&:nts7dt
  • ALERT: You got 0.75 bitcoin! Go to claim → https://graph.org/RECEIVE-BTC-07-23?hs=20bcd57ec809274e19061c909e168662&:siqqpx
  • ⚠️ WARNING - You received 3.0 bitcoin! Go to claim → https://graph.org/RECEIVE-BTC-07-23?hs=d9564a149cf7ebbc725fcfce1bd3d512&:znwd1k
  • WARNING - You received 3.0 BTC! Tap to claim >> https://graph.org/RECEIVE-BTC-07-23?hs=78ee8b109ef1383fa4708aeb263ae5fb&:bwwf6q
  • Account Update - 0.8 Bitcoin pending. Complete reception => https://graph.org/ACCESS-CRYPTO-REWARDS-07-23?hs=9e710a17c6f1893b8975843ad65a53ec&:8d2k3q