It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Partner Sites
Board index Programming JavaScript Forum

On page load, if cookie set then hide div?

On page load, if cookie set then hide div?

Postby purplemonster on Mon Jun 15, 2009 10:15 am

Hi there,

I'm not very good with Javascript 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!
You are wecome here, at my new site http://www.bpled.com
purplemonster
 
Posts: 19
Joined: Fri Jul 04, 2008 10:01 pm

Re: On page load, if cookie set then hide div?

Postby panzer on Tue Jun 16, 2009 10:38 am

Not sure if it's what you're after, but may something like this will help.

Code: Select all
...
<script language="javascript">

function foo() {

   if(document.cookie != "" && document.cookie != null) {
      document.getElementById("d").style.visibility = "hidden";
      alert(document.cookie);
   } else {
      var ndate = new Date();
      ndate.setDate(ndate.getDate() + 1);
      document.cookie = "test=cookie;expires=" + ndate.toGMTString();
      document.getElementById("d").innerHTML = "no cookie";
   }
}

</script>
</head>
<body onload="foo()">
<div id="d"></div>
...


HTH
panzer
 
Posts: 7
Joined: Mon Nov 17, 2008 6:50 am

Re: On page load, if cookie set then hide div?

Postby rangana on Mon Jun 22, 2009 7:44 am

Hi Panzer,

One thing, please avoid using "language" attribute for your script tags. It's deprecated.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 6:14 am
Location: Cebu City Philippines


Who is online

Users browsing this forum: No registered users and 4 guests