PDA

View Full Version : Javascript cookie to show text only once?



Painthappy
09-11-2008, 02:58 PM
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/showthread.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.

jscheuer1
09-11-2008, 04:15 PM
<!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.

Painthappy
09-11-2008, 04:19 PM
That worked fantastic...

Thank you VERY much.

JPW2105
04-05-2013, 01:03 PM
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?

jscheuer1
04-05-2013, 03:28 PM
I'd try:


<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:


<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):


<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.