当用户关闭公告时,请勿再次向用户显示通知

发布于 2025-02-13 04:56:19 字数 649 浏览 0 评论 0原文

我在我的网站上创建了一个通知栏,该通知栏不想在第一次关闭后将其关闭后再次向用户展示。该条按预期工作,但我似乎无法让cookie工作以不再次显示

JS

$(document).ready(function(){
  $(".m-close").click(function(){
      $(".m-bar").hide(600);
  });
});

HTML代码

<center>
  <div class="m-bar m-red">
    <a class="m-microphone"><i class="material-icons" style="font-size:26px;">mic</i></a>
    <a class="m-content" style="color: white;">Something Text</a>
    <a class="m-close" href="#"><i class="material-icons">close</i></a>
  </div>
</center> <br><br>

I have created a notification bar on my site that I don't want to be shown to users again on subsequent visits after they close it the first time. The bar works as expected, but I can't seem to get the cookie to work to not display it again

js

$(document).ready(function(){
  $(".m-close").click(function(){
      $(".m-bar").hide(600);
  });
});

html code

<center>
  <div class="m-bar m-red">
    <a class="m-microphone"><i class="material-icons" style="font-size:26px;">mic</i></a>
    <a class="m-content" style="color: white;">Something Text</a>
    <a class="m-close" href="#"><i class="material-icons">close</i></a>
  </div>
</center> <br><br>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2025-02-20 04:56:19

这是一个代码示例:

function addCookie(name, value, days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

// When clicking the close button
addCookie('hidden', 'yes', 30);

// Checks if the user chose to hide the announcement
const isHidden = getCookie('hidden');

if (isHidden == 'yes') {
   // Hide the announcement
   $(".m-bar").hide(600);
}
else {
   // Show the announcement
   // ...
}

首先,您可以使用AddCookie()函数在关闭公告时添加cookie。

之后,当您显示公告时,请检查cookie 隐藏是否设置为“是”,如果将其设置为是是,然后隐藏公告,否则请显示。

同样,您当然可以使用不同的名称和值以及cookie的到期日期,我建议设置长期到期日期。

Here is a code example:

function addCookie(name, value, days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

// When clicking the close button
addCookie('hidden', 'yes', 30);

// Checks if the user chose to hide the announcement
const isHidden = getCookie('hidden');

if (isHidden == 'yes') {
   // Hide the announcement
   $(".m-bar").hide(600);
}
else {
   // Show the announcement
   // ...
}

First you can use the addCookie() function to add a cookie when you close the announcement.

After that when you display the announcement, check if the cookie hidden is set to yes, if it is set to yes then hide the announcement, otherwise show it.

Also of course you can use different names and values and expiration dates for your cookies, I recommend setting a long expiration date.

蓝眼泪 2025-02-20 04:56:19

我不会选择为此使用cookie,我会使用localstorage,因为这是一个简单的通知:


$(window).on('load', function(){

    //Create a variable for localStorage return
    var confirm = {
        notification: false
    };

    //Get the Data from localStorage
    let dataconfirm = localStorage.getItem('confirm');

    if(dataconfirm != null){

        confirm.notification = dataconfirm;

    }

    console.log(confirm);
  
    $(document).ready(function(){
 

     $('form').on('click', function(e){

        e.preventDefault();

        if(!confirm.notification === true){

            localStorage.setItem('confirm', true);

        }


     });   

  
    });
  
  
  });



I don´t choose use a Cookie for this, I will use the localStorage because is a simple notification:


$(window).on('load', function(){

    //Create a variable for localStorage return
    var confirm = {
        notification: false
    };

    //Get the Data from localStorage
    let dataconfirm = localStorage.getItem('confirm');

    if(dataconfirm != null){

        confirm.notification = dataconfirm;

    }

    console.log(confirm);
  
    $(document).ready(function(){
 

     $('form').on('click', function(e){

        e.preventDefault();

        if(!confirm.notification === true){

            localStorage.setItem('confirm', true);

        }


     });   

  
    });
  
  
  });



~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文