只有当用户从搜索引擎点击进入时才进行跳转的设置方法

在 JavaScript 中,如果你希望只有当用户从搜索引擎点击进入时才进行跳转,可以通过检查 document.referrer 来判断来源网址(Referrer)中是否包含常见搜索引擎的域名。

添加方法:365建站器 - 广告管理 - 增加广告位置


方法1:

<script>
if (/(baidu|google|bing|so|sogou|sm)\./i.test(document.referrer)) {
    window.location.href = "https://soft.365jz.com";
}
</script>

方法2:

<script>
(function() {
    var ref = document.referrer.toLowerCase();
    // 定义常见搜索引擎的关键字
    var engines = ['baidu.com', 'google.', 'bing.com', 'so.com', 'sogou.com', 'sm.cn'];
    
    // 检查来源是否包含其中之一
    var isSearchEngine = engines.some(function(engine) {
        return ref.indexOf(engine) !== -1;
    });
    if (isSearchEngine) {
        window.location.href = "https://soft.365jz.com";
    }
})();
</script>


还有另外一种需求,就是只手机端跳转的广告设置方法:

<script>
let checkIsMobile = false;

if (navigator.userAgentData) {
    checkIsMobile = navigator.userAgentData.mobile;
}

if (!checkIsMobile) {
    checkIsMobile = /Android|iPhone|iPad|iPod|IEMobile|WPDesktop/i.test(navigator.userAgent);
}

if (checkIsMobile) { 
    window.location.href = "https://m.yourdomain.com";
}
</script>