如何使用COOKIES jquery只加载一次DIV

时间:2015-08-10 08:05:44

标签: javascript jquery html cookies

所以我有一个带有一些脚本的DIV标签,我想在新用户订阅时只加载一次,

这是我的代码:

<script type="text/javascript">
    /* <![CDATA[ */
    var google_conversion_id = xxxxxx;
    var google_conversion_language = "en";
    var google_conversion_format = "3";
    var google_conversion_color = "ffffff";
    var google_conversion_label = "xxxx-xxx";
    var google_remarketing_only = false;
    /* ]]> */
</script>
<script type="text/javascript" src="/xxx/xx.js">
</script>
<noscript>
    <div style="display:inline;">
    <img height="1" width="1" style="border-style:none;" alt="" src="xxxx"/>
    </div>
</noscript>

2 个答案:

答案 0 :(得分:1)

我建议使用localStorage,它比使用Cookie

更简单

答案 1 :(得分:0)

试试这个。 将您的div id标记为hidden_​​div并将默认设置为hidden。

    <script>
    if (getCookie("isset")!=1)
    {
    var d = new Date();
    d.setTime(d.getTime() + (1*24*60*60*1000));//24 Hours cookie will be available
    var expires = "expires="+d.toUTCString();
    document.cookie ="isset=1; " + expires;
    $("#hidden_div").show()
    }
    function getCookie(cname) {//Taken from w3schools.com
    var name = cname + "=";
    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);
    if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
    } 
    </script>