Results 1 to 5 of 5

Thread: Javascript cookie to show text only once?

  1. #1
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript cookie to show text only once?

    I'm trying to set up something that will only show text once.

    So I need to create a javascript cookie, that will read it, show the document.write(""), and then never show it again for say 3 days, or 10 days, or whatever the cookie is set for....

    I seen some of code like this one:
    http://www.dynamicdrive.com/forums/s...ad.php?t=28965

    and others, but darn it, I just can't seem to get it to work.

    Can anyone help me in this area? Just a blank page that shows me "TEST TEST TEST" just once, and when I refresh I see nothing, would be a great proof of concept. For whatever reason, I just can't write it and make it work. I know how to do it, but I'm missing something somewhere....

    Thanks in advance folks. It seems simple enough, so I don't know why it has me stumped.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,190 Times in 3,154 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <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);
    }
    </script>
    </head>
    <body>
    <script type="text/javascript">
    if(!readCookie('wroteIt')){
    document.write('Test, Test, Test');
    createCookie('wroteIt', 'wroteIt', 10);
    }
    </script>
    </body>
    </html>
    The red 10 is the number of days persistence.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That worked fantastic...

    Thank you VERY much.

  4. #4
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Can I ask you a question please. I read this with interest as I'm trying to incorporate a pop/up light box that I want to have come up every time someone visits my site, but only every six months and not everytime they move to a new page etc.

    My site has about 100 pages and the light box script is just a one like <script type="text/javascript" src="http://app.getresponse.com/view_webform.js?wid=#####"></script>

    Where should I put that script in the script you've put in above to make this happen?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,190 Times in 3,154 Posts
    Blog Entries
    12

    Default

    I'd try:

    Code:
    <script type="text/javascript">
    if(!readCookie('wroteIt')){
    document.write('<script type="text/javascript" src="http://app.getresponse.com/view_webform.js?wid=#####"></script>');
    createCookie('wroteIt', 'wroteIt', 6 * 30); // about 6 months
    }
    </script>
    Now that should go right where:

    Code:
    <script type="text/javascript" src="http://app.getresponse.com/view_webform.js?wid=#####"></script>
    was, and the other part of the code (which could be made external):

    Code:
    <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);
    }
    </script>
    should go above that. You don't need any of the HTML code from my previous post. That was all simply to make a page to showcase the two scripts on. You already have a page.

    The browser cache may need to be cleared and/or the page refreshed to see changes.

    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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