Skip to content

Latest commit

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

JS-CookGetSet

A simple JavaScript function; variable setter/getter in HTML5 localStorage with cookie fallback. Can be used to save variables of various object types between sessions.

Usage:

cookset(key_name, key_value) --> returns key_value, and sets a key/value pair in localStorage, or as a cookie if unsupported.
cookset(key_name, null) --> returns null, and deletes the pair.
cookget(key_name) --> returns key_value, or null if not set.

Example #1

Batch save/load variables in a session

varvars={variable1, variable2, ...};vars=Object.keys(vars);functionsave(){for(variinvars)cookset(vars[i],window[vars[i]]);cookset("saved",1);}functionload(){if(cookget("saved"))for(variinvars)window[vars[i]]=cookget(vars[i]);}

*Note, I use "$_" as an arbitrary unique prefix to key_name. You can change or delete this in the code to your liking.

About

A simple JavaScript function; variable setter/getter in HTML5 localStorage with cookie fallback.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages