View Full Version : On page load, if cookie set then hide div?

06-15-2009, 11:11 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!

Philip M
06-15-2009, 12:18 PM
This topic has beeen dealt with several times recently in this forum.


One more time:-


<script type="text/javascript">

function createCookie(name,value,days) {
if (days) {
var date = new Date();
var expires = "; expires="+date.toGMTString();
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";

function readCookie(name) {
var nameEQ = name + "=";
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,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
return null;

function eraseCookie(name) {

function setTheDivStyle() {
if(!readCookie('wroteIt')) {
// if cookie not found display the div and create the cookie
createCookie('wroteIt', 'wroteIt', 1); // 1 day = 24 hours persistence
else {
// if cookie found hide the div


<body onload = "setTheDivStyle()">

<div id = "theLink" style="display:block">Your Message Here And You See It Only Once Per 24 Hours</div>


Remember that some people erase their cookies frequently, and others do not have Javascript enabled. The above script will degrade, that is show the div if Javascript is not enabled.

Bookshop Assistant: "This book about Javascript will save you half your work".
Lazy Student: "Oh good! I'll take two!"

06-15-2009, 01:16 PM
Hey Philip, tnx...
but for some reason this wont work in my case...

as you can see:

as you can see the div I want to hide is pop up banner, but it shows every time...
you can view the source code and you'll see it's the same script you gave me..

But now look: if instead of

<div id = "theLink" style="display:none">
<a href="images/index_open.jpg" rel="lightbox" id="img"></a>

I put something like

<div id = "theLink" style="display:none">
Some text

IT WORKS!! It hides that text!

So I guess the problem is in

<a href="images/index_open.jpg" rel="lightbox" id="img"></a>

which refuse to hide somehow!!...

and you realize that I have this link/banner set to autorun on page load trough javascript so that when page loads this link (images/index_open.jpg) opens automatically..

Can you help?