Trying to hide <div> after 1st view using cookie... help!

Discussion in 'JavaScript' started by martinlass, May 22, 2011.

  1. #1
    Hi,
    I'm trying to set up a javascript function to hide a particular <div> after a visitor has viewed once by using a cookie (and setting a 24-hour expiry on this). Here is the code so far, which is not yet working... can anybody help me here, please? What am I doing wrong?

    Cheers,
    Martin

    <html>
    <head>
    <script type = "text/javascript">

    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    var expires = "; expires = "+ date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "="+ value + expires + "; path=/"; // available throughout the directory
    }

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

    function eraseCookie(name) {
    createCookie(name,"",-1); // set cookie to expire on previous day
    }

    </script>
    </head>
    <body onload = 'readCookie("listoffercookie"); createCookie("listoffercookie", "12345", 1")'>

    <script type = "text/javascript">
    if (document.cookie.indexOf("listoffercookie")!=-1) {
    document.getElementById('listoffer').style.visibility = 'hidden';
    }
    </script>

    <div id="listoffer">
    <p>My content...</p>
    </div>
    </body>
    </html>
     
    martinlass, May 22, 2011 IP
  2. martinlass

    martinlass Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Never mind! I figured it out! :)
     
    martinlass, May 22, 2011 IP