ForgejComponent
it

jComponent

Salvare dati nel browser con CACHE, PREF e COOKIES

jComponent 20 · jcomponent@20 jcomponentfrontendstorage Eseguita con jComponent 20

Tre helper salvano dati nel browser:

  • CACHE(key, value, expiration) salva qualsiasi valore per un periodo ('10 minutes', '1 day') e CACHE(key) lo legge, restituendo null una volta scaduto. I dati vivono nell'archivio del browser, quindi sopravvivono a un ricaricamento.
  • PREF.set(name, value) salva una preferenza dell'utente per un anno; rileggila come PREF.name.
  • COOKIES.set(name, value, expiration), COOKIES.get(name) e COOKIES.rem(name) lavorano con cookie veri, che il server riceve a ogni richiesta.
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>
Provalo nel tuo browser

Modifica l'esempio ed eseguilo. Gira in un frame isolato del tuo browser; nulla viene inviato al nostro server.

Anteprima