Jump to content

Anyone good with cookies?


sugafree

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

Link to comment
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?

Link to comment
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;}
Link to comment
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
Link to comment
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.
Link to comment
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
Link to comment
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>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...