Java Reference
In-Depth Information
website developer, can store small amounts of information in a special place on the user's local disk,
using what is called a cookie , and in the browser using HTML5's Web Storage.
Baking Your first Cookie
The key to cookies is the document object's cookie property. Using this property, you can create
and retrieve cookie data from within your JavaScript code.
You can set a cookie by setting document.cookie to a cookie string . You'll be looking in detail at
how this cookie string is made up later in the chapter, but let's first create a simple example of a
cookie and see where the information is stored on the user's computer.
a fresh‐Baked Cookie
The following code sets a cookie with the UserName set as Paul and an expiration date of 28
December, 2020:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fresh-Baked Cookie</title>
<script>
document.cookie =
"UserName=Paul;expires=Tue, 28 Dec 2020 00:00:00;";
</script>
</head>
<body>
<p>This page just created a cookie</p>
</body>
</html>
Save the page as freshbakedcookie.html . You'll see how the code works as you learn the parts of a
cookie string, but first let's see what happens when a cookie is created.
How you view cookies without using code varies with the browser you are using.
Viewing Cookies in Internet explorer
In this section, you see how to look at the cookies that are already stored by Internet Explorer (IE)
on your computer. You then load the cookie‐creating page you just created with the preceding code
to see what effect this has. Follow these steps:
1.
First, you need to open IE. The examples in this chapter use IE 11, so if you're using an ear-
lier version of IE you may find the screenshots and menus in slightly different places.
2.
Before you view the cookies, first clear the temporary Internet file folder for the browser,
because this will make it easier to view the cookies that your browser has stored. Click the
Gear icon and choose the Internet Options menu item, which is shown in Figure 13-1.
You are presented with the Internet Options dialog box shown in Figure 13-2.
 
Search WWH ::




Custom Search