tailieunhanh - Học JavaScript qua ví dụ part 80
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. | 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 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. 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 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 you will see all the cookies set for this page. Figure Using alert From the Library of 702 Chapter 16 Cookies When you reload the page the property will contain all the cookie text saved for that page. Assigning Cookie Attributes To create a cookie assign the name value pairs to the 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 .
đang nạp các trang xem trước