This likely won’t be relevant to a lot of devs here, because the remember plugin does the job fine in most cases, but:
Here’s a normal text input (id
is not needed for this example, but is almost always needed so adding it here):
<input id="thingyInput">
And here’s one which remembers what you type into it even after page refresh:
<input id="thingyInput" oninput="localStorage.thingy=this.value" value="[localStorage.thingy || '']">
Of course, the remember-plugin can do this for you, but I often find myself reaching for the above pattern for its simplicity.
localStorage
is what the remember-plugin
uses behind the scenes - whatever you store in it will be persisted even after page refresh. It’s a built-in browser/JavaScript feature - not something that’s specific to Perchance.
The || ''
in [localStorage.thingy || '']
means or ''
. In other words, it means or output nothing
. If you want a default value for when the user loads the page for the first time, you could write [localStorage.thingy || 'blah']
which means “use whatever is in localStorage.thingy
if it exists, otherwise use ‘blah’”
You could even do something like
localStorage[this.id]=this.value
. Not sure if that would work for getting the value though… 🤔Quick question on this topic… is localStorage “safe”? Does it only store for that specific generator (like, localStorage’s behavior is overridden for that), or can it pick things up and change things from other generators?
Quick question on this topic… is localStorage “safe”?
The
localStorage
is contained inside each generator, meaning that values set from one generator can only be accessible from that generator (like not tied within thelocalStorage
from the entire Perchance website), so it is safe. Basically, every generator has its ownlocalStorage
container, and there’s an explanation to why.Ooooh, okay. So secretly they’re running from their own subdomain, and subdomains have their own localStorage I think. Great 👍
is localStorage “safe”? Does it only store for that specific generator (like, localStorage’s behavior is overridden for that), or can it pick things up and change things from other generators?
Yep, it’s safe. Each generator gets their own subdomain, and browsers partition storage based on subdomain/origin, so one generator cannot read the localStorage of another generator. If you want to do that, then you need to control both generators, and you’d need to embed one within the other, and use postMessage
Thank you 👍
Yeah, it’s just not easy to figure out that actually generators are running in their own subdomain–once someone explained that, I could see how it worked 😁