Last active
May 10, 2020 20:56
-
-
Save hitswa/f15f4a13e89522d73036876f6ee14794 to your computer and use it in GitHub Desktop.
code to set and get cookies in browser using javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *************************************** | |
* LOCAL STORAGE | |
* | |
* Capacity: 10 MB | |
* Browser: HTML5 | |
* Accessible: From any window | |
* Expires: Will last as long as you won't clear / Never | |
* Storage location: Browser only | |
* Sent with requests: No | |
* *************************************** */ | |
function getAllLocalData() { | |
return window.localStorage; | |
} | |
function writeLocal(key, value) { | |
if (localStorageCompatible() ) { | |
localStorage.setItem(key, value); | |
return true; | |
} else { | |
console.log('Browser not compatible, Please contact admin'); | |
} | |
} | |
function readLocal(key) { | |
if (localStorageCompatible()) { | |
return localStorage.getItem(key); | |
} else { | |
console.log('Browser not compatible, Please contact admin'); | |
} | |
} | |
function deleteLocal(key) { | |
if (localStorageCompatible()) { | |
localStorage.removeItem(key); | |
return true; | |
} else { | |
console.log('Browser not compatible, Please contact admin'); | |
} | |
} | |
function clearLocal() { | |
if (localStorageCompatible()) { | |
window.localStorage.clear(); | |
return true; | |
} else { | |
console.log('Browser not compatible, Please contact admin'); | |
} | |
} | |
function localStorageCompatible() { | |
if (typeof (Storage) !== "undefined") | |
return true; | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *************************************** | |
* SESSION STORAGE | |
* | |
* Capacity: 10 MB | |
* Browser: HTML5 | |
* Accessible: Same tab | |
* Expires: will last as long as you don't close tab | |
* Storage location: Browser only | |
* Sent with requests: No | |
* *************************************** */ | |
function getAllSessionData() { | |
return window.sessionStorage; | |
} | |
function writeSession(key, value) { | |
if (sessionStorageCompatible()) { | |
sessionStorage.setItem(key, value); | |
return true; | |
} else { | |
console.log('Browser not compatible, Please contact admin'); | |
} | |
} | |
function readSession(key) { | |
if (sessionStorageCompatible()) { | |
return sessionStorage.getItem(key); | |
} else { | |
console.log('Browser not compatible, Please contact admin'); | |
} | |
} | |
function deleteSession(key) { | |
if (sessionStorageCompatible()) { | |
sessionStorage.removeItem(key); | |
return true; | |
} else { | |
console.log('Browser not compatible, Please contact admin'); | |
} | |
} | |
function clearSession() { | |
if (sessionStorageCompatible()) { | |
window.sessionStorage.clear(); | |
} else { | |
console.log('Browser not compatible, Please contact admin'); | |
} | |
} | |
function sessionStorageCompatible() { | |
if (typeof (Storage) !== "undefined") | |
return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment