Test-M_PC

检测用户访问网站的渠道的JS代码

你可以通过在你的代码中加入这行来应用本JS


                    <script src="https://www.ukldices.cn/tech/js/test-m_pc.js"></script>
                

源码公开

本js我们公开源码

你可以通过本JS了解学习


                    (function() {
                    'use strict';

                    // 获取用户代理字符串
                    var userAgent = navigator.userAgent || navigator.vendor || window.opera;

                    // 定义手机端和电脑端的跳转链接
                    var mobileUrl = "https://m.example.com"; // 替换为手机端的链接
                    var desktopUrl = "https://www.example.com"; // 替换为电脑端的链接

                    // 检查链接是否有效
                    if (!mobileUrl || !desktopUrl) {
                        console.error("Error: mobileUrl or desktopUrl is not defined.");
                        return;
                    }

                    // 检查链接是否是有效的 URL
                    function isValidUrl(url) {
                        try {
                            new URL(url);
                            return true;
                        } catch (e) {
                            return false;
                        }
                    }

                    if (!isValidUrl(mobileUrl) || !isValidUrl(desktopUrl)) {
                        console.error("Error: Invalid URL provided.");
                        return;
                    }

                    // 检测是否为手机端
                    var isMobile = /android|iphone|ipad|ipod|blackberry|iemobile|opera mini|mobile/i.test(userAgent);

                    // 执行跳转
                    try {
                        if (isMobile) {
                            window.location.href = mobileUrl;
                        } else {
                            window.location.href = desktopUrl;
                        }
                    } catch (e) {
                        console.error("Error during redirection:", e);
                    }
                })();
                

代码原则与实践

在代码编写过程中,我遵循以下原则: