Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,750 members, 7,820,587 topics. Date: Tuesday, 07 May 2024 at 05:40 PM

Cookie Programming In Javascript - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Cookie Programming In Javascript (1572 Views)

Arrays In Javascript / Functional Programming In Javascript / Pattern Chaser In Javascript: Coding Challenge (2) (3) (4)

(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).

1 Like

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

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

coz i want some tooo


Lol

crotonite:

FIXED!!!
How?

(1) (Reply)

HTML Is Not A Programming Language? YES Or NO / uPlanIt App for Android: The smart way to plan events / Creating A Simple Calculator Using HTML,CSS And Javascript

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.