Cookiesyou either love them or hate them. Programmers are not usually on the hate-them side, because cookies help the programming process a great deal by enabling you to store data that the browser environmenta notoriously ephemeral environmentcan use. You can customize the appearance of your web site for particular users with cookiesfetching and displaying economic data, for example, or setting background coloror store the data associated with a single session but multiple pages. From the users' point of view, however, you're storing data on their computer. Worse, they have no control over that data. Although cookies have largely been accepted now, there continue to be security issues, including cases of hackers being able to store code in cookies that is executed by the browser (although security holes like this are patched vigorously by browser manufacturers when discovered ). The actual cookie stored by JavaScript code is just text, and it's stored in the document.cookie property. This makes accessing cookies very easy, as we'll see. The actual cookie text itself is made up of name =value; pairs. (Each pair is sometimes called a crumb , and each should end with a semicolon.) If you want to store a value of 555 as a cookie's ID, for example, you might use a pair like this id=555; . In JavaScript, you can store up to 20 such pairs in a single cookie string. Some pairs are already interpreted by the browser, including the following:
The browser stores only one cookie per page. When you reload that page, the browser determines which cookie to give you access to by seeing what page (including the path of that page) you've loaded, and you'll get the whole cookie text for that page in the document.cookie property. You have to search that cookie text for the data pair you want yourself. Let's take a look at an example. |