Answer the question
In order to leave comments, you need to log in
How to use localstorage and cookies?
What is better and how to use?
For example, there is a script that works for a special version, now it adds styles to each tag in the DOM. In order for them to be saved when navigating through the pages, it is necessary to remember them in cookies or localstorage, as far as I understand. Chose the latter.
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
var foo = localStorage.getItem("special-version-js");
// ...
localStorage.setItem("special-version-js", foo);
Answer the question
In order to leave comments, you need to log in
Better localStorage for such purposes.
1. Cookies are stored either temporarily or until the end of the session. localStorage is saved forever.
2. If cookies are disabled by everyone and sundry, localStorage is usually not touched at all.
3. Cookies are difficult to pull out with a script, to parse something there. For localStorage there is getItem
.
4. It is generally impossible to delete a cookie (not to assign "", namely to delete it). For localStorage it will be localStorage.removeItem
.
5. Cookie assignment looks terrible:
document.cookie = 'mynewcookie=1'; // Нет, это не удаление всех кук и создание одной единственной
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question