Store data in the browser with CACHE, PREF and COOKIES
Three helpers store data in the browser:
CACHE(key, value, expiration)stores any value for a period ('10 minutes','1 day') andCACHE(key)reads it, givingnullonce it expired. The data lives in the browser storage, so it survives a reload.PREF.set(name, value)stores a user preference for a year; read it back asPREF.name.COOKIES.set(name, value, expiration),COOKIES.get(name)andCOOKIES.rem(name)work with real cookies, which the server receives on every request.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jComponent</title>
<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
<p class="theme"></p>
<script>
CACHE('profile', { name: 'Peter', age: 42 }, '1 hour');
CACHE('short', 'value', '1 second');
PREF.set('theme', 'dark');
$('.theme').text('Theme: ' + PREF.theme);
COOKIES.set('visited', 'yes', '1 day');
</script>
</body>
</html> Try it in your browser
Edit the example and run it. It runs in an isolated frame in your browser; nothing is sent to our server.
Preview