简体   繁体   English

带有 cookie 的 Javascript 覆盖弹出窗口

[英]Javascript overlay popup with cookies

I'm looking for some advice.我正在寻找一些建议。 I'm trying to create an overlay on an html website with a pop up before a user can proceed to the site.我正在尝试在 html 网站上创建一个带有弹出窗口的叠加层,然后用户才能继续访问该网站。

在此处输入图片说明

So far I've managed to create a cookie:到目前为止,我已经设法创建了一个 cookie:

function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  var expires = "expires=" + d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

With the overlay calling the cookie function after the user clicked Accept用户点击接受后,叠加层调用 cookie 函数

$(function () {
  var overlay = $('<div id="overlay"></div>');
  overlay.show();
  overlay.appendTo(document.body);
  $(".popup").show();
  $(".popupCloseButton").click(function () {
    $(".popup").hide();
    overlay.appendTo(document.body).remove();
    return false;
  });

  $(".x").click(function () {
    $(".popup").hide();
    overlay.appendTo(document.body).remove();
    return false;
  });
  $(".popupAcceptButton").click(function () {
    setCookie("test", "test", 30);
    $(".popup").hide();
    overlay.appendTo(document.body).remove();
    return false;
  });

  $(".x").click(function () {
    $(".popup").hide();
    overlay.appendTo(document.body).remove();
    return false;
  });
});

The popup:弹出窗口:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='popup'>
    <div class='cnt223'>
        <!--<div class="popupCloseButton">&times;</div>-->
        <h1 style="font-size: 18px;">Mholweni, Hello, Goeie dag!</h1>
        <p style="font-size: 14px;">We respect your rights. Please read our Usage Agreement <a href="https://www.saonline.africa/use.html" target="_blank" style="color: #1b305c !important; font-weight: 300;"><u>HERE</u></a>.</p>
        <div class="popupAcceptButton"><font style="font-size: 15px;">I ACCEPT AND WANT TO PROCEED</font></div>
    </div>
</div>

What I'm struggling with is a sort of if else statement to check if the cookie exist, if it does, not to show the popup again.我正在努力解决的是一种 if else 语句来检查 cookie 是否存在,如果存在,则不再显示弹出窗口。

https://stackoverflow.com/a/5968306/14678078 https://stackoverflow.com/a/5968306/14678078

You just have to read from the cookies in the browser, and only call overlay.show() if the cookie doesn't exist, this answer is a perfect example.您只需要从浏览器中的 cookie 中读取,并且仅在 cookie 不存在时调用 overlay.show(),这个答案就是一个完美的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM