sugafree Posted March 27, 2014 Share Posted March 27, 2014 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 More sharing options...
davej Posted March 27, 2014 Share Posted March 27, 2014 You have not explained what you are trying to accomplish. Why do you need to refresh the page? For a cookie example see... http://www.w3schools.com/js/js_cookies.asp Link to comment Share on other sites More sharing options...
justsomeguy Posted March 27, 2014 Share Posted March 27, 2014 The window.location.reload function does in fact reload the page. Link to comment Share on other sites More sharing options...
thescientist Posted March 28, 2014 Share Posted March 28, 2014 he is 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 More sharing options...
sugafree Posted March 28, 2014 Author Share Posted March 28, 2014 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 More sharing options...
sugafree Posted March 28, 2014 Author Share Posted March 28, 2014 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 More sharing options...
sugafree Posted March 28, 2014 Author Share Posted March 28, 2014 function should I use? Sorry went wrong even with copy and paste.. Link to comment Share on other sites More sharing options...
thescientist Posted March 28, 2014 Share Posted March 28, 2014 reload is function. http://www.w3schools.com/jsref/met_loc_reload.asp so that means you have to call it location.reload(); Link to comment Share on other sites More sharing options...
sugafree Posted March 28, 2014 Author Share Posted March 28, 2014 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. Link to comment Share on other sites More sharing options...
sugafree Posted March 28, 2014 Author Share Posted March 28, 2014 Could i use the amt() function to force itself to always refresh and that might solve this problem if it wouldnt crash the page. Is that a really bad idea? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 28, 2014 Share Posted March 28, 2014 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 More sharing options...
sugafree Posted March 28, 2014 Author Share Posted March 28, 2014 (edited) 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 March 28, 2014 by sugafree Link to comment Share on other sites More sharing options...
sugafree Posted March 28, 2014 Author Share Posted March 28, 2014 (edited) 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 March 28, 2014 by sugafree Link to comment Share on other sites More sharing options...
justsomeguy Posted March 28, 2014 Share Posted March 28, 2014 That first piece of code won't set the interval because you're returning from the function before that. Link to comment Share on other sites More sharing options...
davej Posted March 29, 2014 Share Posted March 29, 2014 <!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 More sharing options...
sugafree Posted March 29, 2014 Author Share Posted March 29, 2014 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!=""){ Link to comment Share on other sites More sharing options...
Ingolme Posted March 29, 2014 Share Posted March 29, 2014 The syntax seems fine. What are the exact words of the error message? Link to comment Share on other sites More sharing options...
sugafree Posted March 29, 2014 Author Share Posted March 29, 2014 (edited) 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 March 29, 2014 by sugafree Link to comment Share on other sites More sharing options...
sugafree Posted March 29, 2014 Author Share Posted March 29, 2014 Sorry, it was saved as xhtml, soon as I changed to to html it works fine. Thanks again Link to comment Share on other sites More sharing options...
sugafree Posted March 29, 2014 Author Share Posted March 29, 2014 I sorted out the confirmation box..proud of myself:) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now