Lecture 9
Lecture 9
Types
Create and Store a Cookie
Applications of JS
JS Object model
A cookie is a variable that is stored on the
visitor's computer. Each time the same
computer requests a page with a browser, it
will send the cookie too. With JavaScript,
you can both create and retrieve cookie
values.
Name cookie
Password cookie
Date cookie
• HTTP cookies (also referred to as Web
cookies, tracking cookies, or cookies) are
small text files containing a user's settings
and other data used by websites, and are
stored on the user's computer. They are
transmitted as parcels of text sent by a
server to a Web client (usually a browser)
and then sent back unchanged by client each
time it accesses that server. HTTP cookies
are used for authenticating, session tracking
(state maintenance), and maintaining
specific information about users
• Cookies are not computer programs that run
on the browsing computer
• The pieces of information are stored as name-
value pairs.
• For example, a Web site might generate a unique
ID number for each visitor and store the ID
number on each user's machine using a cookie
file.
• The most common place for them to reside is in
a directory called c:\windows\cookies.
• For example, on visiting goto.com, the site
places a cookie on the machine. The cookie file
for goto.com contains the following information:
• UserID A9A3BECE0563982D www.goto.com
/
The data moves in the following manner:
If you type the URL of a Web site into your browser, your
browser sends a request to the Web site
When the browser does this, it will look on your machine for a
cookie file that Amazon has set. If it finds an Amazon cookie
file, your browser will send all of the name-value pairs in the
file to Amazon's server along with the URL
Web server receives the cookie data and the request for a
page. If name-value pairs are received, it can use them.
If no name-value pairs are received, it knows that you have not
visited before. The server creates a new ID for you in database
and then sends name-value pairs to your machine in the header
for the Web page it sends. Your machine stores the name-value
pairs on your hard disk.
The Web server can change name-value pairs or add new pairs
whenever you visit the site and request a page.
There are other pieces of information that the
server can send with the name-value pair. One of
these is an expiration date. Another is a path
(so that the site can associate different cookie
values with different parts of the site).
a cookie allows a site to store state information
on your machine
Web sites use cookies in many different ways--
Sites can accurately determine how many
people actually visit the site.
Using cookies, sites can determine:
How many visitors arrive
How many are new versus repeat visitors
How often a visitor has visited
Other than being set by a web server, cookies can
also be set by a script in a language such as
JavaScript, if supported and enabled by the Web
browser.
In particular, an internet browser is expected to be
able to store at least 300 cookies of four kilobytes
each, and at least 20 cookies per server or domain.
In practice cookies must be smaller than 4 kilobytes.
Internet Explorer imposes a 4KB total for all cookies
stored in a given domain.
cookie names are case insensitive.
To allow the user to see the cookies that are active
with respect to a given page by typing
javascript:alert(document.cookie) in the browser
URL field.
to access the page http://www.example.org/index.html,
browsers connect to the server www.example.org sending
it a request that looks like the following one:
GET /index.html HTTP/1.1
Host: www.example.org
browser →
server