js判断手机访问跳转到手机站,电脑访问跳转到电脑pc站js源代码 ...

发布者:泡泡淘天 2022-4-11 20:29

js判断手机访问跳转到手机站,电脑端pc访问跳转到pc电脑端js源代码,7种方法实现电脑端访问跳转到电脑端,手机端访问跳转到手机端js代码,跳转js源代码实现方法。

 

第一种方法: 

<script>

 

if(navigator.platform.indexOf('Win32')!=-1){

 

//pc

 

//window.location.href="电脑网址";

 

}else{

 

//shouji

 

window.location.href="手机网址";

 

}

 

</script> 

第二种方法: 

<script type="text/javascript">

 

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= '手机网站地址';

 

} else {

 

window.location= '电脑网站地址';

 

}

 

}

 

browserRedirect();

 

</script> 

第三种方法: 

<script type="text/javascript">

 

var mobileAgent = new Array("iphone", "ipod", "ipad", "android", "mobile", "blackberry", "webos", "incognito", "webmate", "bada", "nokia", "lg", "ucweb", "skyfire");

 

var browser = navigator.userAgent.toLowerCase();

 

var isMobile = false;

 

for (var i=0; i<mobileAgent.length; i++){ if (browser.indexOf(mobileAgent[i])!=-1){ isMobile = true;

 

//alert(mobileAgent[i]);

 

location.href = '手机网址';

 

break; } }

 

</script> 

第四种方法:php网站适用

<?php

 

////这段放手机站里

 

header("Cache-Control: no-cache");

 

header("Pragma: no-cache");

 

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);

 

$uachar = "/(symbianos|android|Mac OS|ucweb|blackberry)/i";

 

if($ua != '' && !preg_match($uachar, $ua)){

 

echo 'window.location.href="电脑地址";';

 

exit();

 

}

 

?>

<?php

 

//这段放在网站里

 

header("Cache-Control: no-cache");

 

header("Pragma: no-cache");

 

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);

 

$uachar = "/(symbianos|android|Mac OS|ucweb|blackberry)/i";

 

if($ua != '' && preg_match($uachar, $ua)){

 

echo 'window.location.href="手机地址";';

 

exit();

 

}

 

?>

第五种方法:php网站适用

<?php

 

function UserAgent(){

 

$user_agent = ( !isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];

 

return $user_agent;

 

}

 

//Mobile

 

if ((preg_match("/(iphone|ipod|android)/i", strtolower(UserAgent()))) AND strstr(strtolower(UserAgent()), 'webkit')){

 

header('Location: 手机网址');

 

exit;

 

}else if(trim(UserAgent()) == '' OR preg_match("/(nokia|sony|ericsson|mot|htc|samsung|sgh|lg|philips|lenovo|ucweb|opera mobi|windows mobile|blackberry)/i", strtolower(UserAgent()))){

 

header('Location: 手机网址');

 

exit;

 

}else{//PC

 

header("Location: 电脑网址");

 

}

 

?>

第六种方法,也可以把名为uaredirect的文件下载到自己网站上引入; 百度siteapp代码

<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script><script type="text/javascript">uaredirect("http://m.429006.com");</script> 

仔细看了下这个js文件:uaredirect.js

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)/i))){location.replace(f)}}}}catch(d){}}function isSubdomain(c,d){this.getdomain=function(f){var e=f.indexOf("://");if(e>0){varh=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(".","\\.");vara=new RegExp("\\."+c+"$");if(b.match(a)){return 2}else{return 0}}}};

 

7.第七种方法 PC跳转手机 与手机跳转到PC

 

这个代码放在PC模板的代码里面 

<script type="text/javascript">

 

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

 

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){

 

}else{

window.location.href="手机网址";

}

</script> 

-----------------------------------------------------------

这个代码放在手机网站模板里面 

<script type="text/javascript">

 

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

 

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="PC网址";

}else{

 

}

</script> 

以上的7种方法都是可以实现PC跳转手机与手机跳转到PC的,可以说是最全的了,可以根据你的需

为你推荐
返回顶部