[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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章