/ Published in: JavaScript
Cleaned up (with the help of jslint.com) versions of cookie handling functions
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function createCookie(name, value, days) { var expires = ""; var date = new Date(); if (days) { date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); } document.cookie = name + "=" + value + expires + "; path=/"; } function readCookie(name) { var nameEQ = name + "="; var allCookies = document.cookie.split(";"); allCookies.forEach(function (cookie) { while (cookie.charAt(0) === " ") { cookie = cookie.substring(1, cookie.length); } if (cookie.indexOf(nameEQ) === 0) { return cookie.substring(nameEQ.length, cookie.length); } }); } function eraseCookie(name) { createCookie(name, "", -1); }
URL: http://www.quirksmode.org/js/cookies.html