美文网首页
JS判断移动设备最佳方法 并实现跳转至手机版网页

JS判断移动设备最佳方法 并实现跳转至手机版网页

作者: 不染事非 | 来源:发表于2019-03-29 15:01 被阅读0次

    方法一 纯js判断

    <script type=”text/javascript”>
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    window.location = “mobile.html”; //可以换成http地址
    }
    </script>
    

    方法二 使用 Device.js 库

    device.js 是一个用于检查设备用的插件,使用它你可以很方便的判断设备的操作系统,以及设备是纵向还是横向。
    下载地址: https://github.com/matthewhudson/device.js
    1.引入device.js

    <script src=”device.min.js”></script>
    
    <script type=”text/javascript”>
    if(device.mobile()){
    window.location = “shouji.html”;  //可以换成http地址
    }
    </script>
    

    相关文章

      网友评论

          本文标题:JS判断移动设备最佳方法 并实现跳转至手机版网页

          本文链接:https://www.haomeiwen.com/subject/hhmlbqtx.html