[JavaScript] 判断客户端类型 实现响应式页面

JavaScript 判断客户端类型 实现响应式页面


在跨平台,各种浏览器,移动设备兼容的时候,经常要根据设备、浏览器做特定调整,所以判断设备和浏览器的工作,我们经常会用到navigator.userAgent.toLowerCase()来进行判断。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <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="https://dengxj.blog.csdn.net/";
                } else {
                    //跳转pc端页面
                    window.location.href="https://dengxj.blog.csdn.net/";
					//一般放在PC页面前端 因此“跳转pc端页面”代码应当删除,不然就陷入了死循环。
                }
            }
            browserRedirect(); 
        </script>
    </head>
    <body>
 
    </body>
</html>

其次就是将代码运用到具体网页中去:

<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
    <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="login-phone.html";
            } else {
                //跳转pc端页面

            }
        }
        browserRedirect();
    </script>

    <title>login</title>
</head>
<body>
网页内容***网页内容***网页内容***网页内容***网页内容***网页内容***网页内容***网页内容***网页内容***网页内容***
</body>
</html>
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页