Đang chuẩn bị liên kết để tải về tài liệu:
Học JavaScript qua ví dụ part 80
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tạo một Cookie với JavaScript Trong các ví dụ sau đây, chúng ta sẽ tạo một cookie, xem các tập tin cookie, và sau đó tiêu diệt nó. Điều quan trọng cần lưu ý khi bạn đang thiết lập cookies mà họ được lưu trữ trong bộ nhớ của trình duyệt và không ghi vào ổ đĩa cứng cho đến khi bạn thoát khỏi trình duyệt. | 16.2 Creating a Cookie with JavaScript 701 Path. The path is used to specify where the cookie is valid for a particular server. Setting a path for the cookie allows other pages from the same domain to share a cookie. FORMAT path pathname EXAMPLE path home Secure. If a cookie is secure it must be sent over a secure communication channel HTTPS server . FORMAT secure 16.2 Creating a Cookie with JavaScript In the following examples we will create a cookie view the cookie and then destroy it. It is important to note when you are setting cookies that they are stored in the browser s memory and not written to the hard drive until you exit the browser. 16.2.1 The Cookie Object The cookie is stored by JavaScript as a document object for both reading and writing cookie data. Cookies are made by assigning attributes to the cookie property. When you start your browser if there are cookies they pertain to the current document. The doc-ument.cookie property contains a string of name value pairs representing the names of all the cookies and their corresponding values such as a session ID number or a user ID. All the other attributes set for the cookie such as expiration date path and secure are not visible. In a JavaScript program if you execute the statement shown in Figure 16.5 you will see all the cookies set for this page. Figure 16.5 Using alert document.cookie From the Library of WoweBook.Com 702 Chapter 16 Cookies When you reload the page the document.cookie property will contain all the cookie text saved for that page. 16.2.2 Assigning Cookie Attributes To create a cookie assign the name value pairs to the document.cookie property. Be careful with quotes making sure the variables you use are not quoted but the text that the cookie needs such as the word name and are quoted. Also this will be a big string where the different parts are concatenated together with the operator. The following format sets a cookie using all possible attributes. Those attributes enclosed in .