Top Banner
Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies
19

Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Jan 12, 2016

Download

Documents

Brent Gardner
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Working with Cookies

Managing Data in a Web Site Using JavaScript Cookies*

*Check and comply with the current legislation regarding handling cookies

Page 2: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Introducing Cookies

• Desired features of a Web site– Users should be able to

• Navigate product listing• Purchase products • Create accounts

Page 3: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Developing a Shopping Cart Application

• Back end – Mechanism that helps Web site function behind

the scenes

• Front end– Visible part of a Web site

Page 4: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Developing a Shopping Cart Application

• Server-side shopping cart– Shopping cart application controlled via server

• Client-side shopping cart – Application controlled via user’s browser with

JavaScript

Page 5: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Client-Side and Server-Side Shopping Cart Applications

Page 6: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Understanding Cookies

• Session– Each visit to a Web page by a user

• Cookie– Text file that stores data from a user’s interaction

with a specific Web site– Persistent

• Stateless protocol– No way to track information

Page 7: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Understanding Cookies

• Client-side cookie– Initiated by a page coming from a Web server – Created in a browser’s memory

• Server-side cookie – Created and stored on a server using server-side

scripting languages

Page 8: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Types of Cookies

• First-party cookie – Cookie created from the Web site you are visiting

• Third-party cookie* – Cookie created at different Web site and is then

sent to Web site you are currently visiting

*Check and comply with the current legislation regarding handling cookies

Page 9: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Customized Cookie Settings in Internet Explorer

Page 10: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Creating a Cookie

• Syntaxdocument.cookie = "name = value; expires = expiration; path = path;

domain = domain; secure";

Page 11: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Cookie Attributes

Page 12: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

The name and value Properties

• Each cookie stores– Single piece of information as its value, which is

paired with a name when cookie is created

• Dynamically generating parameters for cookie

username = document.form1.uname.value;

document.cookie = "input1 = "+username;

Page 13: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

The expires Property

• Cookie – Can be assigned an expiration date

• To assign expiration dateexpires = Day, DD-Mmm-YY HH:MM:SS GMT

• Per session cookie – Exists only for as long as the browser is

communicating with the Web site

Page 14: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

The path Property

• By default– Cookie available only to page where it originated

• path attribute– Used to set the pages to which a cookie is

available

Page 15: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

The domain Property

• Used to specify – URL of domain to which you want to make cookie

available

• If no value is specified for domain property– Its value is set to the server of origin

Page 16: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

The secure Property

• The final property you can set for a cookie• Enables you to specify that

– Cookie is to be transmitted over the HTTPS protocol

• To set a cookie as a secure cookie– Add the parameter “secure” without a value

Page 17: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Populating Form Fields with Cookie Values

• Assigning value of user variable to username field

function retrieveAccount() {

document.login.username.value=user;

}

Page 18: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Using Cookie Values to Create a Personalized Greeting

• Code to personalize the home page<script type="text/javascript">if((fn != "")&&(ln != "")){document.write("Hello, "+fn+" "+ln+". ")}</script>

Page 19: Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.

Deleting Cookies

• To control deletion of cookies– Set date and time you want cookie deleted– Develop function that deletes cookie when it is

called