|
在javascript中使用cookies的例子 |
|
<html>
<head> <title>Cookies in JavaScript</title> <script language="JavaScript"> /***************************************************************************** * These are the basic functions to set, get and delete a cookie. * *****************************************************************************/ function setCookie(name, value, expires) { //Set a cookie given a name, value and expiration date. document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() + "; path=/"; } function getCookie(name) { var search; // Returns the value of the named cookie. search = name + "="; offset = document.cookie.indexOf(search); if (offset != -1) { offset += search.length; end = document.cookie.indexOf(";", offset); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(offset, end)); } else return ""; } function deleteCookie(name) { var expdate = new Date(); // Delete the named cookie. expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); setCookie(name, "", expdate); } </script> </head> <body bgcolor="#ffffff"> <center> <table border=0 cellpadding=8 cellspacing=0 width=550> <tr><td align=center colspan=2> <hr noshade> <font color="#cc6600" face="Arial,Helvetica" size=4><b>Cookies in JavaScript</b></font> <hr noshade> </td></tr> <tr> <td bgcolor="#cccccc" width=100> </td> <td width=450> <font face="Arial,Helvetica" size=2> Embedded within the body of this page is short piece of JavaScript that checks for a cookie named 'lastvisit'. If found, it writes a message to the document showing the date and time stored in that cookie. Then it takes the current date and time and saves them in this cookie. <p> If you reload the page or visit it again later, it will display a timestamp, showing you when you were last here (try hitting your RELOAD button if you don't see it on the next line). <p> <script language="JavaScript"> var last, exp; // Get date and time of last visit from cookie. var last = getCookie("lastvisit"); if (last != "") document.writeln('<b>You last visited this page on ' + last + '.</b>'); // Save current date and time in the cookie. last = new Date(); exp = new Date(); exp.setTime(exp.getTime() + (86400 * 1000 * 30)); setCookie("lastvisit", last, exp); </script> <p> <b>The Cookie Functions</b> <p> <p> There are three functions defined to provide the basic means of handling cookies. These can be used in any script as a way to save information between user visits or retrieve data set in a cookie by the server. <p> <ul> <li><b>setCookie(</b><i>name, value, expires</i><b>)</b> - Creates or updates the named cookie with the given value. The last argument should be a Date object set to the day you want the cookie to expire. <p> <li><b>getCookie(</b><i>name</i><b>)</b> - Returns the value of the named cookie. If the cookie doesn't exist, a null string ("") is returned. <p> <li><b>deleteCookie(</b><i>name</i><b>)</b> - Deletes the named cookie. This is done by setting the expiration date to day before the current date, causing the browser to expire it. </ul> <p> A quick note about expiration dates. You have to supply some date, so the browser knows how long to keep the cookie active. Setting it to the current date will cause the cookie to expire as soon as the user shuts down the browser. In this example, it's always set to last for 30 days. Note these two lines in the script. <p> <pre> exp = new Date(); exp.setTime(exp.getTime() + (86400 * 1000 * 30)); </pre> <p> This creates a new date with a default value equal to the current date and then adds 30 days to it. <p> Date objects are stored internally as integers representing the number of milliseconds from January 1, 1970. The <b>getTime()</b> method will return this value. The <b>setTime()</b> method takes one of these big numbers and sets the Date object to the corresponding date. <p> So, starting with 1000 (one second = 1000 milliseconds), multiplying by 86400 (to get the number of seconds in one day) and again by 30 and adding this to the <b>getTime()</b> value of the current date will result in a date 30 days from today. <p> <b>Browser Differences</b> <p> Different browsers handle cookies differently. Some have restrictions on the number of cookies a site or individual page can set, some may have limits to how much data can be stored in a cookie, etc. And keep in mind that most have options that allow users to disable cookies altogether (although it won't cause an error if you try to use them, you'll just get null values when trying to read them). <p> You should check the documentation for any browsers you expect users to visit your site with. The <a href="/links/documentation.html">Resources</a> section lists some good starting points. In general, you don't have to worry about detecting browser types and versions or creating separate code for different browsers, but just keep in mind any restrictions and desing for the lowest common denominator. This is usually sufficient for anything you need to do. <p> <b>Using Cookies</b> <p> Cookies can be used either on the client-side, as shown here, or on the <a href="/asp/cookies.asp">server-side</a> or using a combination of both. They can be very useful for passing data around in a web-based application so it's worthwhile to learn a little about them. <p> <font color="#cc6600"><b>Source</b></font> <p> <ul> <li><a href="cookielib.html" onclick="location='view-source:' + this.href; return false;">cookielib.html</a> </ul> <p> </font> </td> </tr> <tr><td align=center colspan=2> <hr noshade size=1> <font face="Arial,Helvetica" size=2><a href="/" target="_top">Home</a></font> </td></tr> </table> </center> </body> </html> |