So, You Want To Set A Cookie, Huh? | HTML Goodies

So, You Want To Set A Cookie, Huh?

Written By
Joe Burns
Joe Burns
Jan 4, 2005
8 minute read

Use these to jump around or read it all…


[Why Set Cookies?] [My Cookie Script In Action]
[Setting the Cookie] [What’s It Look Like?]
[Retrieve the Cookie] [More Than One Cookie]


     This tutorial was really a long time coming. People have wanted it for a while, but I’ve been somewhat reluctant to write it for a couple of reasons.

     First, I knew that as soon as I posted something like this I would see people setting cookies all over the place. Cookies are very bad in some people’s eyes. You can read more about that in my original cookie reference piece. But furthermore… these things are hard!

     Setting a cookie using


Why Set Cookies?

     There are two main reasons that I can think of. The first is to “remember” information. Someone comes to your page and offers you his or her name. Next time, you greet them by that name. Clever, and a nice touch. But again, many people are wary of cookies and won’t feel comfortable accepting a cookie just so you can say “hi” at some later date.

     The second reason, and the one I feel is most important, is moving information across pages. Someone chooses something on one page, and when they click to go to the next page, what they chose before is now posted there for them. It’s the whole basis for a shopping cart program.


     First off, this script is one I put together by heavily borrowing from the cookie script submitted by Giedrius. (That’s all…just “Giedrius”)

     Any time you set a cookie, you need to gather some information. Usually it’s done in the format I’m showing here. Someone enters something into a text box, a button is clicked, and the cookie is written that contains the information provided by the reader.

     Once the cookie is written, you can call upon it again and again. Try this:

Enter A Word:


Now… Click to go to another page.


     I should tell you up front, at the moment, the script will not post multiple words in the text box. It’s easily fixed, but I’m trying to keep things pretty simple here, so just enter one word.


Advertisement

     Yes, it’s not that stunning as is, but it gets the point across. If I had you enter your first name, then I could have welcomed you again and again each time you arrived. If this was an order form, the next page would have the number you ordered already posted to the page. Get the picture?

     Let’s start with the setting of the cookie. It’s actually a little easier than the retrieval.


Enter A Word:
onClick=”putCookie()”>


     I know that at this point most people quickly copy and paste the code. When you do, be careful of this line:

document.cookie=cookie_name+”=”+YouEntered+”; expires=Monday, 04-Apr-2010 05:00:00 GMT”;

     The code above is on two lines. That’s bad. Make sure it all goes on one line. The width of the page made it break. Fix that when you paste it.

Start from the bottom…

     When I talk about a script that deals with an HTML form tag, I like to start with them. So, here goes:

Enter A Word:
onClick=”putCookie()”>

     The form creates a text box and a button to fire up the function putCookie(). See that? The NAME of the form itself is “cf” and the NAME of the text box is “cfd”. Now, when we want to grab what is written in the text box, we create the hierarchy statement: document.cf.cfd.value.

     With me? Good. Moving to the script, we’ll take it from top to bottom:

Advertisement

     After hitting the last script, this one should fall together pretty easily. We’ll start after the


     The first simply prints it to the page, the second puts it into a text box. Note the code that creates the text box requires the

tags, too.


     The code you got here is pretty much copy-and-paste easy to install. But let’s say you want to set multiple cookies. You’ll need to remember that the two scripts you got here are links. The first sets the cookie, the second retrieves it. So any changes you make to one script, you’ll need to make to the other.

     First and foremost, you’ll need to set a new cookie name, then new variable names to represent the data. Please note that those variable names appear throughout the script, so make sure you get them all. I do it with my text editor’s “Replace All” function.

     If you grab the cookie the way I did, using a form, then you’ll need to make sure the form element’s NAMEs are correctly represented in the script’s hierarchy statements.

     Don’t sweat it. It’s a little work, but you’ll get it down soon enough. Just remember to keep a backup of the original working scripts. I wasted over an hour this time around by editing the original. That’s what I get for writing a JavaScript book….

Enjoy!


[Why Set Cookies?] [My Cookie Script In Action]
[Setting the Cookie] [What’s It Look Like?]
[Retrieve the Cookie] [More Than One Cookie]


HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.