1. Trang chủ
  2. » Công Nghệ Thông Tin

Học JavaScript qua ví dụ part 81 pps

7 243 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 434,53 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

16.2.5 Deleting a Cookie If you want to delete a cookie for the current page, set the expiration date of the cookie to a date earlier than the current date.. After the cookie has been de

Trang 1

16.2.5 Deleting a Cookie

If you want to delete a cookie for the current page, set the expiration date of the cookie

to a date earlier than the current date This will cause the cookie to be deleted when the

session ends

Figure 16.11 When the user clicks the “See Cookie” button, all cookies are retrieved.

Figure 16.12 After the user clicks the button, the color of the page changes based

on the cookie that was sent.

E X A M P L E 1 6 4

<html>

<head><title>Delete Cookie</title><head>

<script type = "text/javascript">

var i = 0;

2 document.cookie = cookieName + "="

+"; expires=Thu, 01-Jan-1970 00:00:01 GMT";

alert("Cookie was deleted!");

seeCookie();

}

Trang 2

if(document.cookie == ""){

alert("No cookies");

return false;

} else{

4 var myCookie = document.cookie.split("; ");

if ( i < myCookie.length ){

myCookie[i].split("=")[0];

i++; // Increase the index value

// to see the next cookie

} else{alert("No more cookies");}

} }

</script>

</head>

6 <body onLoad="seeCookie()" >

<div align="center">

<h2> Got milk?</h2>

7 <form name="form1">

Is this the cookie you want to delete?

<br />

<input type="text" name="cookietype" >

<p>

8 <input type="radio"

name="radio"

value="choice"

"delCookie(document.form1.cookietype.value);" />Yes

<input type="radio"

name="radio"

value="choice"

10 onClick="seeCookie();" />No

</p>

</form>

</div>

</body>

</html>

E X P L A N A T I O N

1 The function called delCookie() will remove a requested cookie The name of the

cookie, cookieName, is passed as a parameter to the function.

Continues

E X A M P L E 1 6 4 (C O N T I N U E D)

Trang 3

2 The expiration date of the cookie is set to the beginning of UNIX time, also called

epoch time—January 1, 1970 Certainly this date has passed and the cookie will

be deleted After the cookie has been deleted, the seeCookie() function will be

called, and the user will be presented with another cookie If he or she clicks the

Yes radio button, that cookie will be removed

3 The function called SeeCookie() will check to see if there are any cookies

remain-ing in the document.cookie property If not, the program is over To actually see if

the cookies were deleted, close this session, and then reopen it

4 By splitting up the document.cookie property by semicolons, an array is created

consisting of a name and value attribute of the cookie.

5 The first element of the array, the name of the cookie, is assigned to the textbox

represented as document.form1.cookietype.value It will appear in the textbox for

the user to see Each time the function is called, the next cookie will be assigned

to the textbox, giving the user the option to delete that cookie

6 When the document has finished loading, the onLoad event is triggered, and calls

the seeCookie() function The first cookie name will appear in the textbox.

7 The HTML form starts here

8 The textbox input type will be used to hold the cookie name

9 This radio button, when clicked, will called the delCookie() function The user

wants to remove this cookie See Figures 16.13 and 16.14

10 This radio button, when clicked, means the user doesn’t want to delete this cookie

but would like to see the next cookie When the user clicks No, the seeCookie()

function will be called After all the cookies have been shown, the alert message

will say “No more cookies.”

Figure 16.13 The cookie’s name is visitor If the user clicks Yes (top), the cookie will

be removed (bottom).

E X P L A N A T I O N (C O N T I N U E D)

Trang 4

16.2.6 Using the Browser to Remove Cookies

Another way to delete cookies is to go in your browser and select Help and search for

Delete cookies In Internet Explorer go to the Tools menu and Internet Options (see

Figure 16.15) Then you can remove all or some cookies from the hard drive

Figure 16.14 The cookie has been deleted after user clicked the “Yes” button.

Figure 16.15 Internet Options in Internet Explorer.

Trang 5

16.3 What You Should Know

Now you know what cookies are and why commercial sites like to use them You should

know:

1 A little about the history of the cookie

2 How to make a cookie

3 How to use a cookie

4 What escaping a cookie value means

5 When the cookie normally expires if an expiration date is not set

6 How to delete a cookie

7 How to delete cookies using your browser

8 How to enable or disable cookies on your browser

9 How JavaScript views a cookie

Trang 6

1 Create a form that contains a set of checkboxes with different types of coffees—

espresso, cappuccino, mocha, and so on Ask the user for his or her name and room number and to select a type of coffee Tell the user you will be sending the coffee to that room number Create a cookie to remember the user’s preference

The next time the user brings up the page, tell him or her there is a special rate for his or her (use the cookie value) favorite coffee

2 Create a form that asks for the user’s shipping address The next time the user

brings up the page, fill in the address using a cookie value

E x e r c i s e s

Trang 7

ptg

Ngày đăng: 04/07/2014, 02:20

TỪ KHÓA LIÊN QUAN