₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,073 members, 8,420,169 topics. Date: Thursday, 04 June 2026 at 12:44 PM

Toggle theme

Cookie Programming In Javascript - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingCookie Programming In Javascript (1733 Views)

1 Reply (Go Down)

Cookie Programming In Javascript by Nobody: 7:56am On Jul 06, 2015
In 2009, I created a thread on cookies - https://www.nairaland.com/215381/how-store-data-javascript-javascript#3309239
It helped a lot of people, but due to questions a lot of people were asking, I just created another sample code to use the same cookie class (I did not modifiy the original class mind you).

<html>
<title>My Great Cookie Example</title>
<script>
/*
Cookie class written by Anthony Ogundipe from www.facebook.com/dhtml5
*/
function jcookies() {}
jcookies.prototype.online=function() {return location.href.indexOf('http')==-1 ? false : true;}
jcookies.prototype.set=function (cname,cvalue) {
if(this.online()) {document.cookie = cname + '=' + escape(cvalue) + '; expires=Mon, 31 Dec 2099 23:59:59 UTC;path=/;';}
else {document.cookie = cname + '=' + escape(cvalue) + '; expires=Mon, 31 Dec 2099 23:59:59 UTC;';}
}
jcookies.prototype.erase=function(cname) {
if(this.online()) {document.cookie = cname + '=' + '' + '; expires=Mon, 31 Dec 1899 23:59:59 UTC;path=/;';}
else {document.cookie = cname + '=' + "" + '; expires=Mon, 31 Dec 1899 23:59:59 UTC;';}
}
jcookies.prototype.get=function(sName) { var aCookie = document.cookie.split('; ');
for (var i=0; i < aCookie.length; i++) {
var aCrumb = aCookie[i].split('=');
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}
return '';
}
//cookie class stops


var cook=new jcookies();

function savex() {
cook.set('meal',document.getElementById('food').value);
alert('Information saved');
}

function loadx() {
document.getElementById('food').value=cook.get('meal');
}

function erasex() {
document.getElementById('food').value='';
cook.erase('meal');
alert('Information erased');
}

</script>

<p style='line-height:6mm'>
This page gives you an example of how cookies work. <br/>
Step 1: You can type any text you like, click on save to save it to cookies.<br/>
Step 2: You can click on the load button to retrieve your cookie.<br/>
Step 3: Refresh the page (or reopen the browser), and press load again (it will still load the last value)<br/>
Step 4: You can click on the erase button to erase your cookie.<br/>
</p>

Text: <input type="text" id="food">
<input type="button" value="Save" onclick="savex()">
<input type="button" value="Load" onclick="loadx()">
<input type="button" value="Erase" onclick="erasex()">
So here it is once again. And do remember, though HTML5 has local storage, but using cookies cuts across all browsers (that have cookies enabled).
Re: Cookie Programming In Javascript by jaelz(m): 10:26am On Aug 17, 2015
Nice stuffs your doing please give me your number
Re: Cookie Programming In Javascript by Nobody: 10:33am On Aug 17, 2015
Just go to facebook.com/dhtml5 o jare.
Re: Cookie Programming In Javascript by JavaPrince1(m): 2:33am On Nov 08, 2015
Javascript has always been my worst friend. I need to know about the jcookies class members and member functions so that I can understand the cook object. Thank you.
Re: Cookie Programming In Javascript by Booyakasha(f): 5:53am On Nov 08, 2015
Keep sharing stupid codes... lipsrsealed
Re: Cookie Programming In Javascript by Nobody: 7:47am On Nov 08, 2015
I shall take the credit for that.
Re: Cookie Programming In Javascript by Nobody: 8:30pm On Nov 22, 2015
is it the edible cookie huh

coz i want some tooo
Re: Cookie Programming In Javascript by Nobody: 8:33pm On Nov 22, 2015
Booyakasha:
Keep sharing stupid codes... lipsrsealed
FIXED!!!
Re: Cookie Programming In Javascript by Nobody: 9:03pm On Nov 22, 2015
crotonite:
is it the edible cookie huh

coz i want some tooo
Lol

crotonite:
FIXED!!!
How?
1 Reply

Arrays In JavascriptFunctional Programming In JavascriptPattern Chaser In Javascript: Coding Challenge234

Help: External CSS File Not Working With My PHP FilesCan I Do Data Analysis On An Android PhoneA 19 Year Old Me Gave An Advice That Doesn't Help Me 3 Years Later