JS-如果存在cookie,则隐藏div [英] JS - Hide div if cookie exists

查看:105
本文介绍了JS-如果存在cookie,则隐藏div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,在这里,我只想让客户每30天显示一次div。如果没有cookie,将显示div,当单击div时,div将执行与此处无关的操作,然后将存储一些cookie(例如客户端IP)。如果存在cookie,尽管div会消失。我在这里写了一些代码,检查是否可以解决问题,并帮助我解决问题。

Here, basically, I want to let a client show the div only once in 30 days. If no cookies are present the div would be shown, and when clicked, the div would do something, irrelevant here, and then it would store some cookies, (e.g. client IP). If the cookies are present though the div would disappear. I have written some code here, check it out if you can, and help me solve the problem.

<script type="text/javascript" src="http://l2.io/ip.js?var=myip"></script>
<script>
        var el = document.getElementsByClassName('popup');

if(!document.cookie) {
    el.style.display = 'block';
    setCookie("clientip", myip, 30);
} else {
    el.style.display = 'none';
}

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;
}
</script>

我如何在此处为弹出窗口设置监听器?

How would I set a listener here, for "popup"?

顺便说一句,第一个脚本真的很酷,我发现它可以通过JS返回客户端IP。

BTW, the first script is something really cool I found for returning clients IP through JS.

推荐答案

使用jQuery;)

使用 $。cookie( cookietitle, cookievalue); 并查看它是否已设置并隐藏该元素,请执行以下操作:

Set your cookie with $.cookie("cookietitle", "cookievalue"); and to see if it is set and to hide the element, do this:

$(document).ready(function() {
     var CookieGet = $.cookie('yourcookie');
     if (CookieGet != null) {
          // Hide the element here.
          $('.yourelement').hide();
     }
 });

这篇关于JS-如果存在cookie,则隐藏div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆