Download presentation
Presentation is loading. Please wait.
Published byBranden Fleming Modified over 9 years ago
1
Cookies (continue)
2
Extracting Data From Cookies Data retrieved from a cookie is a simple text string. While there is no specific JavaScript function for parsing the information from the text string, you can build a function using the indexOf() method. The text string returned from the cookie contains only the name=value pairs of information. var myCookie = document.cookie;
3
Create a Multiple Parameter Cookie Enter A User Name function getName(form) { var cookieExp = new Date(); var year = cookieExp.getTime() + (365 * 24 * 60 * 60 * 1000); cookieExp.setTime(year); var name = escape(form.field1.value); document.cookie = "userName=" + name + "; expires=" + cookieExp.toGMTString()+ "; path=/"; document.location='nextpage.htm'; } Please type in a user name:
4
nextpage.htm Continue Your Visit Thank you! document.write(" " + unescape(document.cookie) + " ")
5
Cookie with interactive web page Extracting Cookie Data Shopping.htm Shopping2.htm Shopping3.htm
6
Summary Cookies are widely used on the WEB They make tracking information about the user practical There are two types of cookies: Temporary – no expiration date Stored – future expiration date Deleting a cookie in JavaScript is accomplished by setting its expiration date to an earlier date than now Cookies can be updated Cookie information must be encoded using the escape() method and decoded using the unescape() method.
7
Practice Step 1: Making a Cookie // insert your Javascvript code here
8
Practice Step 2: create a form that looks like this picture (replace this Tshirt picture with any TShirt picture downloaded from google image search)
9
Practice Step 3: Use examples shown today in class to write a function create a cookie that contains “tshirt=white Tshirt”. Call this function when the user click on “Add to shopping cart” button Step 4: Use examples shown today in class to write a function view this cookie. Call this function when the user click on “View cart” button.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.