pc跳移动端
//方案一
if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
//触屏手机版地址
var url = window.location.href.replace("http://www","http://m");
window.location.href = url;
}else if(/iPad/i.test(navigator.userAgent)){
//pad版地址
var url = window.location.href.replace("http://www","http://m");
window.location.href = url;
}else{
//普通手机版地址
var url = window.location.href.replace("http://www","http://m");
window.location.href = url;
}
}
catch(e){
}
}
//方案二
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
window.location.href = "移动端地址";
}
//移动端跳pc
var isOnPc=!(/Android|webOS|iPhone|iPod|BlackBerry|BB10|Symbian|Nokia|UCWEB|IEMobile|opera mini/i.test(navigator.userAgent));
//上面这个表示不是移动端
//下面这句话的意思是不是移动端的时候跳转到相应的地址
isOnPc && (location.href="地址");
js双端互跳
最新推荐文章于 2024-08-27 09:41:07 发布
本文介绍了两种不同的方案,一是通过navigator.userAgent检测实现PC向移动端的自动跳转,包括针对不同设备类型的适配;二是直接检测用户代理字符串来判断是否为移动端并进行相应跳转。这些技术对于响应式设计和用户体验至关重要。
摘要由CSDN通过智能技术生成