ForgejComponent
en

jComponent

Store data in the browser with CACHE, PREF and COOKIES

jComponent 20 · jcomponent@20 jcomponentfrontendstorage Executed with jComponent 20

Three helpers store data in the browser:

  • CACHE(key, value, expiration) stores any value for a period ('10 minutes', '1 day') and CACHE(key) reads it, giving null once 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 as PREF.name.
  • COOKIES.set(name, value, expiration), COOKIES.get(name) and COOKIES.rem(name) work with real cookies, which the server receives on every request.
public/index.html
<!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