Change localStorage to sessionStorage

This commit is contained in:
Andrew Dinh 2021-06-08 00:26:23 -07:00
parent 19fe768aa1
commit 3fe00bfd96
Signed by: andrewkdinh
GPG Key ID: 2B557D93C6C08B86
4 changed files with 4 additions and 10 deletions

View File

@ -2,7 +2,7 @@
Extremely simple contact form with a CAPTCHA. Entries are sent to the specified HTTP endpoint.
JavaScript is not required to fill out the form, but if it is, then form fields are cached in localStorage.
JavaScript is not required to fill out the form, but if it is, then form fields are cached in sessionStorage.
## Building

View File

@ -32,7 +32,7 @@ def index():
message = message.replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;")
if message != "":
requests.post(HTTP_ENDPOINT, data={'subject': 'New Simple Contact message', 'message': message})
return render_template('message.html', message = "Your message was sent successfully", success=True)
return render_template('message.html', message = "Your message was sent successfully")
else:
raise TypeError("Invalid method")
except Exception as e:

View File

@ -43,13 +43,13 @@
Array.from(document.querySelectorAll('form .' + fc)).forEach(element => {
let elementName = fc + '-' + location.pathname + '#' + element.id
// Load from cache
let cached = localStorage.getItem(elementName)
let cached = sessionStorage.getItem(elementName)
if (cached != null) {
element.value = cached
}
// Auto save to cache
element.addEventListener('input', function () {
localStorage.setItem(elementName, element.value)
sessionStorage.setItem(elementName, element.value)
})
})
})()

View File

@ -30,12 +30,6 @@
<a href="/">Go back to homepage</a>
{% endif %}
{% if success %}
<script>
localStorage.clear();
</script>
{% endif %}
<script defer>
let elems = document.getElementsByClassName("script-only");
elems[0].classList.remove("script-only");