Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    On page load, if cookie set then hide div?

    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!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This topic has beeen dealt with several times recently in this forum.

    http://www.codingforums.com/showthread.php?t=167832
    http://www.codingforums.com/showthread.php?t=168836

    One more time:-

    Code:
    <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=/";
    }
    
    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) {
    	createCookie(name,"",-1);
    }
    
    function setTheDivStyle() {
    if(!readCookie('wroteIt')) {
    // if cookie not found display the div and create the cookie
    document.getElementById("theLink").style.display="block";
    createCookie('wroteIt', 'wroteIt', 1);  // 1 day = 24 hours persistence
    }
    else {
    // if cookie found hide the div
    document.getElementById("theLink").style.display="none";
    }
    }
    </script>
    
    </head>
    
    <body onload = "setTheDivStyle()">
    
    <div id = "theLink" style="display:block">Your Message Here And You See It Only Once Per 24 Hours</div>
    
    </body>
    </html>
    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!"
    Last edited by Philip M; 06-15-2009 at 11:29 AM. Reason: Correction

  • Users who have thanked Philip M for this post:

    GuitarLord (06-15-2009)

  • #3
    New Coder
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hey Philip, tnx...
    but for some reason this wont work in my case...

    as you can see:
    www.topfm.rs/index_lightbox.html

    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

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

    Code:
    <div id = "theLink" style="display:none">
    Some text
    </div>
    IT WORKS!! It hides that text!

    So I guess the problem is in
    Code:
    <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?
    Last edited by GuitarLord; 06-15-2009 at 12:42 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •