How to store Slider origin in JavaScript session storage

Asked 1 years ago, Updated 1 years ago, 314 views

I want to save the range input slider's discharge (the last position on the slider) in session storage and let the last release value appear when the web page is refreshed. I don't know exactly because I just started.

I googled and found out what to do with json.stringfy.Can you tell me? Is there anyone who has it?

javascript html css

2023-01-31 21:23

1 Answers

https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

sessionStorage is written like this:

sessionStorage.setItem('key', 'value');
let a = sessionStorage.getItem('key');
console.log(a); // value

If the value you want to put in is not a string but an object type, write JSON.stringify() to create a string, save it, and return it to the object when ejecting:

let obj = { a: 1, b: 2 };
sessionStorage.setItem('foo', JSON.stringify(obj));
let obj2 = JSON.parse(sessionStorage.getItem('foo'));
console.log(obj2); // Object { a: 1, b: 2 }

I can use this to save the range input slider's value and take it out, right?


2023-01-31 22:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.