JavaScript Local-Browser Non-Expiring Cookie (Public Domain)
March 2017

Here’s some free (public domain) code I wrote in JavaScript, that stores and retrieves a persistent (non-expiring) local-browser cookie:

//  Store the user’s name.
if (userNameForCookie.indexOf(';')<0
&&  userNameForCookie.indexOf('=')<0) {
  document.cookie='MySlickAppUser='+userNameForCookie+'; expires=Fri, 31 Dec 9999 23:59:59 GMT'; }

//  Fetch the user’s name.
var userName='', c='; '+document.cookie+'; ', pos=c.indexOf('; MySlickAppUser=');
if (pos>=0) {
  c=c.substring(pos+17); pos=c.indexOf('; '); if (pos>=0) userName=c.substring(0,pos); }

Back to Tutorials.