Jump to content
Sign in to follow this  
sugafree

Anyone good with cookies?

Recommended Posts

Hi, I am trying to amend a cookie script. I have found this online and when I hit the delete cookies button, I have to refresh the page to see it. How could I do this as the deleteCookie function is resetting the exp time which does it but what do I need to add afterwards to make it refresh automatically?

 

after document.cookie = line

function DeleteCookie (name) { var exp = new Date(); exp.setTime (exp.getTime() - 1);  var cval = GetCookie (name); document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();   <--}

 

i tried location.reload(); and window.reload(); but wasnt working.

 

Any idea? Here is the full code

 

<script LANGUAGE="JavaScript">function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) {var j = i + alen;   if (document.cookie.substring(i, j) == arg)     return getCookieVal (j);   i = document.cookie.indexOf(" ", i) + 1;   if (i == 0) break;  } return null;}function SetCookie (name, value) { var argv = SetCookie.arguments; var argc = SetCookie.arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : null; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) +   ((secure == true) ? "; secure" : "");}function DeleteCookie (name) { var exp = new Date(); exp.setTime (exp.getTime() - 1);  var cval = GetCookie (name); document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();window.reload();}var expDays = 30;var exp = new Date();exp.setTime(exp.getTime() + (expDays*24*60*60*1000));function amt(){var count = GetCookie('count')if(count == null) {SetCookie('count','1')return 1}else {var newcount = parseInt(count) + 1;DeleteCookie('count')SetCookie('count',newcount,exp)return count   }}function getCookieVal(offset) {var endstr = document.cookie.indexOf (";", offset);if (endstr == -1)endstr = document.cookie.length;return unescape(document.cookie.substring(offset, endstr));}// End --></script></head>   <body><script LANGUAGE="JavaScript"><!-- Begindocument.write("You have visited this page for <b>" + amt() + " times.")// End --></script><input type="button" onclick="DeleteCookie('count')" value="Delete Cookies"/> </body>

 

 

 

Thanks

Share this post


Link to post
Share on other sites

he is

cookie-monster.jpg

 

 

in all seriousness though, are you checking for errors? how are you sure the page isn't getting refreshed?

Share this post


Link to post
Share on other sites

Ive tried

 

 

function DeleteCookie (name) { var exp = new Date(); exp.setTime (exp.getTime() - 1);  var cval = GetCookie (name); document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + restart();}function restart () {window.location.reload;}

Share this post


Link to post
Share on other sites
also tried the reload in the DeleteCookie function, didnt work either. What I need to accomplish is when you click delete cookies, a confirmation window appears and if ok clicked the hit counter should display 1 without having to refresh the page. If I run this code it wont do it. What sort of functi

Share this post


Link to post
Share on other sites

Its still not working. I tried it a few different ways with reload() and every time the browser starts refreshing itself and just wont stop and wont display anything on the page.

Share this post


Link to post
Share on other sites

I tried it a few different ways with reload() and every time the browser starts refreshing itself and just wont stop and wont display anything on the page.

It sounds like you put the call to reload in a place that causes a reload loop.

Share this post


Link to post
Share on other sites

This wont work either. Could I set the interval for the function which displays the number?

var delay = 500;var expDays = 30;var exp = new Date();exp.setTime(exp.getTime() + (expDays*24*60*60*1000));function amt(){var count = GetCookie('count')if(count == null) {SetCookie('count','1')return 1setInterval(amt,delay);}

 

<body><script LANGUAGE="JavaScript"><!-- Begindocument.write("You have visited this page for <b>" + amt() + " times.")// End --></script><input type="button" onclick="DeleteCookie('count')" value="Delete Cookies"/>
Edited by sugafree

Share this post


Link to post
Share on other sites

or could I just refresh a specific div in the body? that case I put that in a div and.... dont know the rest but at least Ive got an idea:)

Edited by sugafree

Share this post


Link to post
Share on other sites

That first piece of code won't set the interval because you're returning from the function before that.

Share this post


Link to post
Share on other sites
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>cookie count</title><script>var cnt;window.onload = init;function init(){document.getElementById('btn1').onclick = expireCookie;getCookie('count');setCookie('count',(cnt+1));}function getCookie(cookie_name) {var str;var cookie_val = null;var doc_cookie_str = document.cookie;var idx = doc_cookie_str.indexOf(cookie_name + "=");if (idx != -1){  idx = doc_cookie_str.indexOf("=", idx);  var idx2 = doc_cookie_str.indexOf(";", idx);  if (idx2 == -1){    idx2 = doc_cookie_str.length;  }  cookie_val = unescape( doc_cookie_str.substring(idx+1,idx2) );  cnt = Number(cookie_val);  str = 'found:['+ cookie_name +'='+ cookie_val +']'; }else{  cnt = 0;  str = 'initializing:[ no valid cookie found ]'; }document.getElementById('out1').innerHTML = str;}function setCookie(cookie_name,cookie_val){// cookies cannot contain commas, semicolons, or whitespacevar expire_days = 3;if (cookie_name!=null && cookie_name!="" && cookie_val!=null && cookie_val!=""){  var exp_date = new Date();  exp_date.setDate(exp_date.getDate() + expire_days);  var cookie_value = escape(cookie_val) + ((expire_days==null) ? "" : "; expires="+exp_date.toUTCString());  document.cookie = cookie_name + "=" + cookie_value;  var str = '<br/>set:['+ cookie_name +'='+ cookie_value +']';  document.getElementById('out1').innerHTML += str;}}function expireCookie(){cookie_val = '-';var expire_days = -1;var cookie_name = 'count';if (cookie_name!=null && cookie_name!="" && cookie_val!=null && cookie_val!=""){  var exp_date = new Date();  exp_date.setDate(exp_date.getDate() + expire_days);  var cookie_value = escape(cookie_val) + ((expire_days==null) ? "" : "; expires="+exp_date.toUTCString());  document.cookie = cookie_name + "=" + cookie_value;  //alert('set:['+ cookie_name +' set to expired]');} document.getElementById('out1').innerHTML = 'Cookie has been expired.';}</script></head><body><input type="button" id="btn1" value="Expire Cookie"/><div id="out1"></div></body></html>

Share this post


Link to post
Share on other sites

Thanks Davej. I tried it but it wasnt working. In the function setCookie, it says this line is not well-formed

 

 

if (cookie_name!=null && cookie_name!="" && cookie_val!=null && cookie_val!=""){

Share this post


Link to post
Share on other sites

The syntax seems fine. What are the exact words of the error message?

Share this post


Link to post
Share on other sites

The error message is not well-formed and pointing this line. Tried it with mozilla, that one said not well-formed. IE not displaying anything.

Edited by sugafree

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...