This example is a little helper class to make and erase cookies in javascript. I will first post the Cookies.js script that will create, read, and erase cookies from your browser.
Cookies.js
/*
author: Copypasteearth 7/17/2017
*/
function COOKIE () {
this.createCookie = function (name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
,
this.readCookie = function (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var 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 null;
}
,
this.eraseCookie = function(name) {
this.createCookie(name,"",-1);
}
}
Then you just include this javascript in a webpage and create the object and then you can call the different functions as you will. Here is an example HTML file using it. This file just pretty much tests out the Cookies.js file.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>profile</title>
</head>
<body>
<script src="Cookies.js"></script>
<h1>this is a test of Cookies</h1>
<h2 id = "cook"></h2>
<input id = "cookiename" type="text" placeholder="cookie name..."></input>
<input id = "cookievalue" type="text" placeholder="cookie value..."></input>
<input id = "days" type = "number" placeholder="number of days"></input>
<button onclick="buttonClick()">Create Cookie</button>
<br>
<input id = "getcookie" placeholder="cookie name"></input>
<button onclick="getCookie()">Read Cookie</button>
<br>
<input id = "erase" placeholder="cookie name"></input>
<button onclick="deleteCookie()">Erase Cookie</button>
<p id= "paragraph"></p>
<script>
var Cookie = new COOKIE();
function buttonClick(){
var name = document.getElementById("cookiename").value;
var value = document.getElementById("cookievalue").value;
var days = document.getElementById("days").value;
Cookie.createCookie(name,value,days);
document.getElementById("paragraph").innerText = "Cookie Created: " + name + "=" + value + ";" + days;
}
function getCookie(){
var name = document.getElementById("getcookie").value;
var cookie = Cookie.readCookie(name);
document.getElementById("paragraph").innerText = "Cookie Read: " + name + "=" + cookie;
}
function deleteCookie(){
var name = document.getElementById("erase").value;
Cookie.eraseCookie(name);
document.getElementById("paragraph").innerText = "Cookie Erased: " + name;
}
</script>
</body>
</html>
and that’s pretty much it for this article. Maybe you can get some use out of this. I hope you enjoyed this article.
HP 14 Laptop, Intel Celeron N4020, 4 GB RAM, 64 GB Storage, 14-inch Micro-edge HD Display, Windows 11 Home, Thin & Portable, 4K Graphics, One Year of Microsoft 365 (14-dq0040nr, Snowflake White)
$172.16 (as of December 13, 2025 22:51 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire 7 Kids tablet (newest model) ages 3-7. Top-selling 7" kids tablet on Amazon. Includes ad-free and exclusive content, easy parental controls, 10-hr battery, 16 GB, Purple
$54.99 (as of December 13, 2025 22:51 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire HD 8 Kids Pro tablet (newest model), ages 6-12. Bright 8" HD screen, includes ad-free content, parental controls, 13-hr battery, slim case for older kids, 32GB, Hello Teal
$149.99 (as of December 13, 2025 22:51 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Dell Gaming OptiPlex Desktop RGB Computer PC, Intel Core i7, GeForce GT 1030 2GB GDDR5, 16GB RAM, 512GB SSD, 24 Inch HDMI Monitor, Keyboard Mouse and Headset, WiFi, W11 Pro (Renewed)
$489.99 (as of December 13, 2025 22:51 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)200 PCS Funny Holographic Stickers for Adults, Waterproof Vinyl Sarcastic Meme Decals for Laptop, Water Bottle, Phone, Kindle, Journal, Scrapbook, Bumper, Skateboard, Luggage, No Repeats
$11.99 (as of December 13, 2025 22:51 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Author: John Rowan
I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.
