Retrieving Cookies

The world's simplest way to see what a cookie is the following:

document.write(document.cookie)

The document.cookie contains all of the cookies in the current domain and path. Let's take a closer look at this in Example 16-3.

Example 16-3. Retrieving cookies
<html>
<head>
    <title>cookie see</title>
</head>
<body>
<script language="JavaScript">
document.cookie="user=Irving"
					document.cookie="userID=21234"
					document.write(document.cookie)
</script>
</body>
</html>

HOW THE CODE WORKS

As you can see, we're creating two cookies, and then we're writing the cookie object to the browser window. Figure 16-1 shows what this looks like.

Figure 16-1. Writing a cookie

As you can see, the following string was displayed:

 user=Irving; ...

Get Advanced JavaScript™: Insights and Innovative Techniques now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.