js双端互跳

本文介绍了两种不同的方案,一是通过navigator.userAgent检测实现PC向移动端的自动跳转,包括针对不同设备类型的适配;二是直接检测用户代理字符串来判断是否为移动端并进行相应跳转。这些技术对于响应式设计和用户体验至关重要。
摘要由CSDN通过智能技术生成
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="地址");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值