School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,131 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,055 people online right now. Registration is fast and FREE... Join Now!




JavaScript Cookies

 
Reply to this topicStart new topic

> JavaScript Cookies, Set, Read, Reset, and View Cookies

kewlkreator
Group Icon



post 9 Aug, 2009 - 01:31 PM
Post #1


Hello. In this tutorial, I'm going to show you how to use cookies in JavaScript.

Cookie?
A cookie is a small text file that contains information that can be written/read by a website. These text files may be no larger than 4kb or about 4000 characters.

Write a Cookie
To write a cookie, we can use the document.cooke statement. Lets write a cookie that stores a username:
CODE
function writeCookie() {
var username = prompt("Whats your username?", "kewlkreator");
//Gets the username
var cookieContent = "username=" + escape(username);
//Escapes the username to remove any illegal characters
document.cookie = cookieContent;
//Sets the cookie
}


Read a Cookie
To read a cookie, I'm gonna split the cookie into an array.
CODE
function readCookie() {
var wholeCookie = document.cookie;
//Grabs the whole cookie
var splitCookie = wholeCookie.split("=");
//Splits the whole cookie into an array called splitCookie
var username = splitCookie[1];
//Where splitCookie[0] is "username", splitCookie[1] holds our string
var username = unescape(username);
//Unescapes the username
alert("Your username: " + username);
//Alerts the username
}


Reset a cookie
To reset a cookie, pass the new content through a parameter like:
CODE
function setCookie(value1, value2) {
//Value 1 is the name of value2
//Value 2 is the content of value1
var cookieContent = escape(value1) + "=" + escape(value2);
//Escapes to remove all illegal characters
document.cookie = cookieContent;
}


So, to use this, we could call the function like setCookie(myname, kewl) this would set a cookie like:
document.cookie = myname=kewl

_______________________________________
<--||Extra: Alert a site's cookie--||>
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

If you want to read a site's cookie, enter into the address bar: java script:alert(document.cookie);

This will alert the cookie content! smile.gif

Finished
Your done! Go forth my minions! Conquer the world of cookies! biggrin.gif

--Kewlkreator
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 02:39PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month