Shinori Posted August 23, 2019 Share Posted August 23, 2019 (edited) Dear forumusers, I'm currently having trouble with deleting my browsercookies. Specifically speaking im trying to delete the cookies of the website the user is currently on. Here are my JS functions + the proof for existing cookies. function setCookie(cname, cvalue, days) { var dt, expires; dt = new Date(); dt.setTime(dt.getTime()+(days*24*60*60*1000)); expires = "; expires="+dt.toGMTString(); document.cookie = cname+"="+cvalue+expires+';'; } function delCookie(name) { document.cookie = name + '= ; expires = Thu, 01 Jan 1970 00:00:00 GMT'; } function logout() { var cookie = getCookie("member"); if(typeof cookie != "undefined" || typeof cookie != null || cookie.length > 0) { delCookie("member"); window.location.reload(); } else { alert("Beim Logout ist etwas schief gelaufen!"); window.location.reload(); } } Apparently my browsers language isn't english. Sorry for that. Basically it says that the cookies name is "member", that is has content and expires in 1 day. I call the "logout"-function via onclick event in visible DOM. The functions gets called successfully. The cookie existence of "member" is then successfully validated and delcookie() is called. The windows gets reloaded as logout() is suppose to do. Eventually the cookie is NOT being deleted. Any ideas whats going wrong her? Thanks in advance! Sorry for my bad english :S Edited August 23, 2019 by Shinori Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2019 Share Posted August 23, 2019 Does it clear out the value after you try to delete it? Link to comment Share on other sites More sharing options...
Shinori Posted August 23, 2019 Author Share Posted August 23, 2019 21 minutes ago, justsomeguy said: Does it clear out the value after you try to delete it? Negative. The cookie is exactly the same after trying to delete it 😕 Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2019 Share Posted August 23, 2019 Try this instead: document.cookie = name+'=; Max-Age=-99999999;'; Link to comment Share on other sites More sharing options...
dsonesuk Posted August 23, 2019 Share Posted August 23, 2019 Just one question, where's getcookie() function? Link to comment Share on other sites More sharing options...
Shinori Posted August 23, 2019 Author Share Posted August 23, 2019 13 minutes ago, justsomeguy said: Try this instead: document.cookie = name+'=; Max-Age=-99999999;'; Didn't change anything. The cookie still hasn't been deleted. 13 minutes ago, dsonesuk said: Just one question, where's getcookie() function? Here it is: function getCookie(cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for(var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2019 Share Posted August 23, 2019 Are you running this locally or with a web server? Link to comment Share on other sites More sharing options...
Shinori Posted August 23, 2019 Author Share Posted August 23, 2019 Im running it on a webserver. Plesk to be precise. I tried it cross-browser and it didnt change it either 😕 Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2019 Share Posted August 23, 2019 I don't see any issues, as far as I'm aware that should work fine. Link to comment Share on other sites More sharing options...
dsonesuk Posted August 24, 2019 Share Posted August 24, 2019 setcookie expires = "; Remove semicolon Should be expires = "expires="+ d.toUTCString(); 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