<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>检测设备并跳转</title>
</head>
<body>
    <script>
        //判断是否为移动端
        function isMobile() {
            var ua = navigator.userAgent;
            return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile Safari|Windows Phone/i.test(ua);
        }
<pre><code>    //判断是否为安卓设备
    function isAndroid() {
        var ua = navigator.userAgent;
        return /Android/i.test(ua);
    }

    //判断是否为电脑设备
    function isPC() {
        var ua = navigator.userAgent;
        return /Windows NT|Macintosh/i.test(ua);
    }

    //判断是否为苹果设备
    function isIOS() {
        var ua = navigator.userAgent;
        return /iPhone|iPad|iPod/i.test(ua);
    }

    //判断是否在微信浏览器中打开
    function isWechat() {
        var ua = navigator.userAgent.toLowerCase();
        return /micromessenger/i.test(ua);
    }

    //根据不同的设备,跳转到不同的页面
    if (isMobile()) {
        //移动端
        if (isAndroid()) {
            //安卓设备
            window.location.href = 'http://your-android-link.com';
        } else if (isIOS()) {
            //苹果设备
            window.location.href = 'http://your-ios-link.com';
        } else {
            //其他移动设备
            window.location.href = 'http://your-mobile-link.com';
        }
    } else if (isPC()) {
        //电脑设备
        window.location.href = 'http://your-pc-link.com';
    } else {
        //其他设备
        window.location.href = 'http://your-unknown-link.com';
    }

    //如果在微信浏览器中打开,则提示用户在浏览器中打开
    if (isWechat()) {
        alert('请在浏览器中打开链接');
    }
&lt;/script&gt;
</code></pre>
</body>
</html>
JavaScript 检测设备类型并跳转 - 移动端、安卓、电脑、苹果、微信

原文地址: https://www.cveoy.top/t/topic/mF85 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录