10 examples of 'store object in localstorage' in JavaScript

Every line of 'store object in localstorage' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
21public setLocalObject(key: string, obj: any): boolean {
22 return this.setObject(this.localStorage, key, obj);
23}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

69saveObject(key, value) {
70 this.db.insert(this.prefix + key, JSON.stringify(value));
71}
31public setObject(key: string, value: any): void {
32 this.localStorage[key] = JSON.stringify(value);
33}
54function store(storage, key, value) {
55 if (!storage) { throw new Error("Storage object is undefined"); }
56
57 // get
58 if (value === undefined) {
59 var item = storage.getItem(key);
60 return item && JSON.parse(item);
61 // set
62 } else {
63 try {
64 storage.setItem(key, JSON.stringify(value));
65 } catch (err) {
66 // prevent catching old values
67 storage.removeItem(key);
68 }
69 }
70}
23export function store(key: string, value: any) {
24 const bak = context.getItem(key);
25 context.setItem(key, JSON.stringify(value));
26 return bak;
27}
21function store(key, value, persist) {
22 if ($.exists(value) && $.isObject(value)) {
23 value = "!!stringified!!" + JSON.stringify(value);
24 }
25
26 if (persist) {
27 localStorage.setItem(key,value);
28 } else {
29 sessionStorage.setItem(key, value);
30
31 if ($.exists(localStorage.getItem(key))) {
32 localStorage.removeItem(key);
33 }
34 }
35}
2export function store(key: string, value: string): void {
3 try {
4 if (window.localStorage) {
5 window.localStorage.setItem(key, value);
6 }
7 }
8 catch (ex) {
9 }
10}
27public setObject(key: string, value: any): void {
28 this.sessionStorage[key] = JSON.stringify(value);
29}
129setLocal(key: string, value: string) {
130 if (this._supportsLocal) {
131 localStorage.setItem(key, value);
132 }
133}
96_saveLocalStorage(data: any) {
97 store.set(LOCAL_STORAGE_NAME, data);
98}

Related snippets