网站重要通知公告代码

放上这个代码后进入网站会先跳转到这个公告页面,要慎用.....

1.创建一个a.js将以下js代码放入:

function setCookie(name, value, days) {
    let date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); // 1天有效
    document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + date.toUTCString() + "; path=/";
}

function getCookie(name) {
    let cookies = document.cookie.split('; ');
    for (let i = 0; i < cookies.length; i++) {
        let cookiePair = cookies[i].split('=');
        if (cookiePair[0] === name) return decodeURIComponent(cookiePair[1]);
    }
    return null;
}
function saveReferrer() {
    let referrer = document.referrer; 
    if (referrer && referrer.indexOf(window.location.origin) !== -1) {        
        localStorage.setItem("sourcePage", referrer);
    }
}

document.addEventListener("DOMContentLoaded", function () {
    saveReferrer(); 

    if (!getCookie("jumpedToday")) {
        setCookie("jumpedToday", "true", 1); // 设置 1 天有效
        window.location.href = "/b.html"; // 替换成你的公告页面
    }
});

2.创建一个b.html将以下代码放入(文件名称如果更改需要在a.js里更改文件名称);

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>公告页面</title>
    <style>
        body {
            text-align: center;
            font-family: Arial, sans-serif;
            margin-top: 50px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 10px;
            box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #ff6600;
        }
        .btn {
            display: block;
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            font-size: 16px;
            text-decoration: none;
            color: white;
            background-color: #007bff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .btn:hover {
            opacity: 0.8;
        }
        .btn.secondary {
            background-color: #28a745;
        }
        .btn.third {
            background-color: #ff6600;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>重要通知</h1>
    <p>本页面一天弹出一次</p>
    
    
    <button class="btn" onclick="goToNewDomain1()">发布页 1</button>
    <button class="btn secondary" onclick="goToNewDomain2()">发布页 2</button>
    <button class="btn third" onclick="goToNewDomain3()">发布页 3</button>
    
    <hr>
    <button class="btn secondary" onclick="goBack()">返回来源页面</button>
</div>

<script>
    // 页面加载时保存来源页面 URL
    window.onload = function() {
        const referrer = document.referrer;
        if (referrer && referrer !== window.location.href) {
            localStorage.setItem("sourcePage", referrer);
        }
    };

    function goToNewDomain1() {
        window.location.href = "https://baidu.com"; // 替换为你的新域名1
    }

    function goToNewDomain2() {
        window.location.href = "https://baidu.com"; // 替换为你的新域名2
    }

    function goToNewDomain3() {
        window.location.href = "https://baidu.com"; // 替换为你的新域名3
    }

    function goBack() {
        const sourcePage = localStorage.getItem("sourcePage") || document.referrer;
        
        if (sourcePage && sourcePage !== window.location.href) {
            window.location.href = sourcePage; 
        } else {
            window.location.href = "/"; 
        }
    }
</script>

</body>
</html>

3.调用代码;<script src="/a.js"></script>   放在需要跳转的html页面

版权声明:图图 发表于 2025-02-11 16:19:20。
转载请注明:网站重要通知公告代码 | 站长资源站Pro|站长资源|建站教程

暂无评论

暂无评论...