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

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Hide div using cookie if page has been already loaded today?

Hide div using cookie if page has been already loaded today?

Postby purplemonster on Fri Apr 24, 2009 7:01 am

Hi,
First, I am absolute javascipt newbie so please threat me like that.. :)))

I need the following functionality to my webpage:

Let's say I have a div with some unique ID on my page.
Now, what I need is to show this div on my page only the first time visitor opens a page, and then not show it every next time he/she opens it for next 24h.

So I need to set a cookie that hides div from page if it has already been shown to visitor, for next 24h, so that the content of this div will be shown to each unique visitor only once for a day.

Thanks in advance... ;)

Nikola
purplemonster
 
Posts: 24
Joined: Fri Jul 04, 2008 9:01 pm

Re: Hide div using cookie if page has been already loaded today?

Postby dflynn on Fri Apr 24, 2009 2:25 pm

I would suggest using PHP for this. That way, if the user is new it can be generated on the spot. If not, then it will not be executed in the html and just be hidden.

Code: Select all

if( isset($_COOKIE['myCookie'])){

//Nothing will happen because the Cookie is set. If not it will go to else:

}else{
echo <div class="myDiv">This content only appears if the Cookie called myCookie is set in the user's browser.</div>
}


Something like that should work before your headers are loaded.
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: Hide div using cookie if page has been already loaded today?

Postby purplemonster on Fri Apr 24, 2009 6:42 pm

dflynn wrote:I would suggest using PHP for this. That way, if the user is new it can be generated on the spot. If not, then it will not be executed in the html and just be hidden.

Code: Select all

if( isset($_COOKIE['myCookie'])){

//Nothing will happen because the Cookie is set. If not it will go to else:

}else{
echo <div class="myDiv">This content only appears if the Cookie called myCookie is set in the user's browser.</div>
}

Something like that should work before your headers are loaded.



Heeey, thanks for idea!! :)

Well, I currently have this in my BODY, but it wont work :(
Code: Select all

<?php
if(isset($_COOKIE['cookiename']))
{ echo "cookie set";
}
else
{ echo
?>
<div id="autoload">
Cookie not set
</div>
<?php
;}
?>


<?php
setcookie("cookiename", "someValue", time()+3600);
?>


And when I view it in browser, and then refresh no matter how many times I get "Cookie not set".

What am I doing wrong?
purplemonster
 
Posts: 24
Joined: Fri Jul 04, 2008 9:01 pm

Re: Hide div using cookie if page has been already loaded today?

Postby dflynn on Fri Apr 24, 2009 7:23 pm

Well, I currently have this in my BODY, but it wont work :(
Code: Select all

<?php
if(isset($_COOKIE['cookiename']))
{ echo "cookie set";
}
else
{ echo
?>
<div id="autoload">
Cookie not set
</div>
<?php
;}
?>


<?php
setcookie("cookiename", "someValue", time()+3600);
?>


Your code is off. it should read:
Code: Select all
<?php
if(isset($_COOKIE['cookiename']))
{ echo "cookie set";
}
else
{ echo "<div id=\"autoload\">Cookie not set</div>"
;}

setcookie("cookiename", "someValue", time()+3600);
?>
I apologize my code was wrong in my first reply.

User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada


Who is online

Users browsing this forum: psbot [Picsearch] and 4 guests