Setting cookies is very simple and you can display them all with just one line of code
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands On: How to set and check Javascript cookies

Cookies can be used as an alternative to pop-ups on your website

Nigel Whitfield, Personal Computer World 30 Apr 2008
ADVERTISEMENT

Previously I've explained how you can use pop-up windows on your website, launching them from a short piece of Javascript.

But, of course, many users aren’t keen on pop-ups, even if they remind them of something quite important.

So it’s a compromise and sometimes part of that compromise might be not displaying a pop-up for a certain amount of time, such as seven days. A solution for handling that is to use cookies.

The cookie monster
Some people have an aversion to cookies, mistakenly believing that they can allow your computer to send private information to a website.

That’s not really the case ­ they can only send back to a website some information that it sent to your computer in the first place, like your user id on the site, or something that helps them see which user has looked at which pages.

A cookie consists of a few bits of data, stored as text on your computer. There’s a name, a value for the data ­ perhaps your id for the site, or a set of preferences, or simply ‘true’ or ‘false’; anything that can be saved as text ­ an expiry date, a path and a domain.

Those last three are the elements that we’re interested in here.

The Expiry date is the date on which the browser can safely discard the cookie. If it’s not set, then the cookie is a ‘session cookie’ which means it’ll disappear when you quit the browser, otherwise it’ll be stored until the date, which is specified in a fairly strict format.

The path restricts the cookie to a section of your website. If it’s set to ‘/’ then the cookie will be returned to the web server for every page on your site. Set it to, say, ‘/shop’ and only pages on your site with URLs starting ‘/shop’ will see its value.

The domain works in a similar way, but can restrict the cookie to part of your domain, or those higher up. If you have a domain mysite.example, then setting the cookie to that would allow it to be seen on websites at forums.mysite. example, or www.mysite.example.

Set it to forums.mysite.example, and the main website won’t get the cookie, either.

Cookies in Javascript
So, now you know how cookies work, how are they handled in Javascript? Not elegantly would be one way of putting it. In a PHP script, and other languages, you can easily see the value of any single cookie. Not so in Javascript.

First, though, how about setting a cookie? All you do is set a value called document.cookie to whatever you want, like this, which sets a cookie called ‘popupshown’ to the value ‘true’:

document.cookie = ‘popupshown=true; expires=Thu, 14 Feb 2007 12:00:00
UTC; path=/’

You could add a domain too ­ it’s just another parameter, which could follow the path option, after a semicolon. Omit the expiry information and you just get a session cookie; set it to a date in the past and you’ll delete the cookie.


All Software Applications
Tags: Hands On, Internet, Javascript

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
London, United Kingdom | University of London (Institute of Education)
 INSTITUTE OF EDUCATION University of London Systems Administrator (London Knowledge Lab) Computing and Media Support Salary in the range £28,290 - £33,780 per annum, plus £2,323 London Allowance  Job share considered We are seeking to ... more >
Sutton, Surrey, United Kingdom | Royal Marsden Hospital NHS Trust
  The Royal Marsden NHS Foundation Trust is a centre of excellence for research, development, education and care in the treatment of cancer. Analyst Programmers, Band 6, £23,458-£31,779 plus 15% HCAS, Sutton, Surrey We are ... more >
Hertfordshire, United Kingdom | SMART
 Business Development Executive, Hertfordshire, £20,000-£22,000 per annum OTE £34 -36k The role: An exciting opportunity has become available within a prestigious organisation with aggressive growth plans. We are looking to recruit an office based Business ... more >
London, United Kingdom | Shell
 Site Systems Integration Manager, London, United Kingdom Shell Downstream encompasses all the activities necessary to transform crude oil into petroleum products and petrochemicals, and deliver them around the world.   Our Downstream businesses refine, supply, ... more >
More job opportunities