常用的访问pc自动跳转到移动页面的代码
1
2
3
4
5
6
7
8
9
10
11
12
|
( function () { if (/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile|Opera Mini/i.test(navigator.userAgent)) { var siteName = window.location.pathname; if (url.indexOf( "?pc" ) < 0) { try { if ( typeof siteName !== "undefined" ) { window.location.href = "https://m.umxmt.com" + siteName } } catch (e) {} } } })(); |
访问移动端自动跳pc端的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
;( function () { var reWriteUrl = function (url) { if (url) { var Splits = url.split( "/" ), siteName = window.location.pathname; if ( typeof siteName !== "undefined" ) { return "https://www.umxmt.com" + siteName } } }; if (!/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile|Opera Mini/i.test(navigator.userAgent)) { var url = window.location.href; var pathname = window.location.pathname; if (url.indexOf( "?m" ) < 0) { try { window.location.href = reWriteUrl(url) } catch (e) {} } } })(); |
判断浏览器是否为手机端
1
2
3
|
<script type= "text/javascript" > ( function (){ var reWriteUrl= function (url){ if (url){ var Splits=url.split( "/" ),siteName=window.location.pathname; if ( typeof siteName!== "undefined" ){ return "https://m.umxmt.com" +siteName}}}; if (/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){ var url=window.location.href; var pathname=window.location.pathname; if (url.indexOf( "?pc" )<0){ try {window.location.href=reWriteUrl(url)} catch (e){}}}})(); </script> |
判断浏览器是否为pc端,是就跳到pc页面
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
|
<script> var browser1 = { versions: function () { var u = navigator.userAgent, app = navigator.appVersion; return { //移动终端浏览器版本信息 trident: u.indexOf( 'Trident' ) > -1, //IE内核 presto: u.indexOf( 'Presto' ) > -1, //opera内核 webKit: u.indexOf( 'AppleWebKit' ) > -1, //苹果、谷歌内核 gecko: u.indexOf( 'Gecko' ) > -1 && u.indexOf( 'KHTML' ) == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile/i) || !!u.match(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/), //是否为移动终端 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf( 'Android' ) > -1 || u.indexOf( 'Linux' ) > -1, //android终端或者uc浏览器 iPhone: u.indexOf( 'iPhone' ) > -1 || u.indexOf( 'Mac' ) > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf( 'iPad' ) > -1, //是否iPad webApp: u.indexOf( 'Safari' ) == -1 //是否web应该程序,没有头部与底部 }; } (), language: (navigator.browserLanguage || navigator.language).toLowerCase() } if (browser1.versions.mobile== false &&browser1.versions.ios== false &&browser1.versions.android== false &&browser1.versions.iPhone== false &&browser1.versions.iPad== false ){ //location.href = location.href.replace("m.","www.");//如果不是手机打开,则跳转到pc页面 alert( "pc" ); } </script> |
以下是补充可以参考
jQuery判断浏览器是移动端还是电脑端自动跳转
一个段小代码,同一个网站针对移动端查看和电脑端查看跳转不同的页面。
首先加载jQuery文件。
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
|
$( function (){ var MobileUA = ( function () { var ua = navigator.userAgent.toLowerCase(); var mua = { IOS: /ipod|iphone|ipad/.test(ua), //iOS IPHONE: /iphone/.test(ua), //iPhone IPAD: /ipad/.test(ua), //iPad ANDROID: /android/.test(ua), //Android Device WINDOWS: /windows/.test(ua), //Windows Device TOUCH_DEVICE: ( 'ontouchstart' in window) || /touch/.test(ua), //Touch Device MOBILE: /mobile/.test(ua), //Mobile Device (iPad) ANDROID_TABLET: false , //Android Tablet WINDOWS_TABLET: false , //Windows Tablet TABLET: false , //Tablet (iPad, Android, Windows) SMART_PHONE: false //Smart Phone (iPhone, Android) }; mua.ANDROID_TABLET = mua.ANDROID && !mua.MOBILE; mua.WINDOWS_TABLET = mua.WINDOWS && /tablet/.test(ua); mua.TABLET = mua.IPAD || mua.ANDROID_TABLET || mua.WINDOWS_TABLET; mua.SMART_PHONE = mua.MOBILE && !mua.TABLET; return mua; }()); //SmartPhone if (MobileUA.SMART_PHONE) { // 移动端链接地址 document.location.href = 'http://www.umxmt.com/wap/index.html' ; } }); |
需要手机端也网页的数据同步
<script type="text/javascript">
(function(){var ua=navigator.userAgent.toLowerCase();var bIsIpad=ua.match(/ipad/i)=="ipad";var bIsIphoneOs=ua.match(/iphone os/i)=="iphone os";var bIsAndroid=ua.match(/android/i)=="android";var bIsWM=ua.match(/windows mobile/i)=="windows mobile";if(bIsIpad||bIsIphoneOs||bIsAndroid||bIsWM){window.location.href="http://m.umxmt.com/android/game/826.html"}})();
</script>
(function(){var ua=navigator.userAgent.toLowerCase();var bIsIpad=ua.match(/ipad/i)=="ipad";var bIsIphoneOs=ua.match(/iphone os/i)=="iphone os";var bIsAndroid=ua.match(/android/i)=="android";var bIsWM=ua.match(/windows mobile/i)=="windows mobile";if(bIsIpad||bIsIphoneOs||bIsAndroid||bIsWM){window.location.href="http://m.umxmt.com/android/game/826.html"}})();
</script>
第二种方法:
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://www.umxmt.com" ); </SCRIPT> |