1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

On page load, if cookie set then hide div?

Discussion in 'C#' started by Error404, Jun 14, 2009.

  1. #1
    Hi there,

    I'm not very good with ASP so I need you help for page on my website...

    Basically what I need to do is to make a DIV shown only first time someone visits page, and then when he/she revisit it I want that same DIV hidden, and I want it hidden for next 24 hours... so in that way DIV would be shown only once a day, and only on first page load.

    So to make that happen I need to do fallowing, and I would need your help for that: I need a script that on a page load checks if cookie (which is 24h cookie) is set, if it is then it should hide that DIV, if it is not set then it should set it, so that DIV would be hidden on next page load...

    Can someone help me with this, cause I am getting mad trying to do this whole day :)))

    Thanks people!
     
    Error404, Jun 14, 2009 IP
  2. dopanel.com

    dopanel.com Peon

    Messages:
    93
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
      If request.cookies("Shown")<>"yes" then
        Response.Cookies("Shown")="yes"
        Response.Cookies("Shown").Expires = DateAdd("h", 24, Now())
        ShowIng the DIV Here!
      end if
    
    Code (markup):
    Or you can using js in client browser!
     
    dopanel.com, Jun 14, 2009 IP
  3. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #3
    This can be implemented using javascript.
    The following code check whether a cookie with a specified name is available.
    If available, then it hides a div.
    If not, then it creates a new cookie with expiry period as 1 day.

    Quote:
    <html>
    <head>

    <script language="javascript">

    function createCookie(name,value,days) // fn. for creating cookies
    {
    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=/";
    }

    function readCookie(name)
    {
    var flag = 0;

    var dcmntCookie = document.cookie.split(';'); // getting all cookies in the document and splitting them and storing in an array

    for(var i=0;i < dcmntCookie.length;i++) // looping through the cookies array
    {
    var ck = dcmntCookie;
    while (ck.charAt(0)==' ') // loop for removing space at the begining
    {
    ck = ck.substring(1,ck.length);
    }
    if(ck)
    {
    cparts = ck.split('='); // splitting the cookie. eg: mycookie="some message"

    if (cparts[0]==name)
    {

    flag=1;
    document.getElementById('header').style.display="none"; // hiding the div
    break;
    }
    }

    }

    if(!flag) // if no such cookie exist, then create a new one
    createCookie(name,"cookie 4 the day",1);
    }

    </script>

    </head>

    <body onLoad="readCookie('MyCookie')">

    <div id="header"> Cookie is now set, Refresh (or close and open) the page to see that.</div>

    </body>

    </html>
    Hope this code helps. :)
     
    Unni krishnan, Jun 15, 2009 IP