javascript - Deleting cookies function is not working -
i want delete cookies, code works upto , set cookies,,, deleting of cookie not working please suggest me answer
code
<body> <select id="themeselect" onchange="savetheme(this.value);"> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> <option value="4">four</option> <option value="5">five</option> </select> <button type="button" onclick="showcookie();">getcookie</button> <button type="button" onclick="deletecookie(name);">deletecookies</button> <script> var saveclass = null; function savetheme(cookievalue) { var sel = document.getelementbyid('themeselect'); saveclass = saveclass ? saveclass : document.body.classname; document.body.classname = saveclass + ' ' + sel.options[sel.value].innerhtml.replace(" ", "").tolowercase(); setcookie('theme', cookievalue, 365); } function setcookie(cookiename, cookievalue, ndays) { var today = new date(); var expire = new date(); if (ndays == null || ndays == 0) ndays = 1; expire.settime(today.gettime() + 3600000 * 24 * ndays); document.cookie = cookiename + "=" + escape(cookievalue) + ";expires=" + expire.togmtstring(); } function getcookie(name) { var re = new regexp(name + "=([^;]+)"); var value = re.exec(document.cookie); return (value != null) ? unescape(value[1]) : null; } function showcookie() { var = getcookie(name); alert(a); } function deletecookie(name) { if (getcookie(name)) { var cookie_date = new date(); // current date & time cookie_date.settime(cookie_date.gettime() - 1); document.cookie = cookiename += "=; expires=" + cookie_date.togmtstring(); } } </script> </body>
function deletecookie(name) { if (getcookie(name)) { var cookie_date = new date(); // current date & time cookie_date.settime(cookie_date.gettime() - 1); document.cookie = name + "=; expires=" + cookie_date.togmtstring(); } }
cookiename undefined in scope. change name
, parameter passed function , should work.
also use +
concatenate strings, had additional =
there prevented working.
@ this fiddle see in action.
Comments
Post a Comment