Skip to content

Instantly share code, notes, and snippets.

@hitswa
Last active May 10, 2020 20:56
Show Gist options
  • Save hitswa/f15f4a13e89522d73036876f6ee14794 to your computer and use it in GitHub Desktop.
Save hitswa/f15f4a13e89522d73036876f6ee14794 to your computer and use it in GitHub Desktop.
code to set and get cookies in browser using javascript
/* ***************************************
* COOKIE STORAGE
*
* Capacity: 4kb
* Browser: HTML4/5
* Accessible: From any window
* Expires: Wll last as per number of days provided / Manual Set
* Storage location: Browser and server
* Sent with requests: Yes
* *************************************** */
function writeCookie(name,value,days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}else{
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var i, c, ca, nameEQ = name + "=";
ca = document.cookie.split(';');
for(i=0;i < ca.length;i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return '';
}
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
// setting cookie value
var sId = 's234543245';
writeCookie('sessionId', sId, 3);
// getting cookie value
var sId = readCookie('sessionId');
/* ***************************************
* 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;
}
/* ***************************************
* 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