/ Published in: JavaScript
Simple functions to add, fetch or delete cookies in the browser via JavaScript.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function setCookie(c_name,c_value,c_expiredays,c_path,c_domain) { var c_exdate=new Date(); c_exdate.setDate(c_exdate.getDate()+c_expiredays); document.cookie=c_name+ "=" +escape(c_value)+((c_expiredays==null) ? "" : ";expires="+c_exdate.toUTCString()) +"; path="+ c_path +"; domain="+ c_domain; } function getCookie(tag) { var value = null var myCookie = document.cookie + ";" var findTag = tag + "=" var endPos if (myCookie.length > 0) { var beginPos = myCookie.indexOf(findTag) if (beginPos != -1) { beginPos = beginPos + findTag.length endPos = myCookie.indexOf(";",beginPos) if (endPos == -1) endPos = myCookie.length value = unescape(myCookie.substring(beginPos,endPos)) } } return value } function deleteCookie(cookie) { var yesterday = 24 * 60 * 60 * 1000 var expireDate = new Date() expireDate.setTime (expireDate.getTime() - yesterday) document.cookie =cookie + "=" + escape("nothing") + ";" + "expires" + "=" + expireDate.toGMTString() }