网摘:http://hi.baidu.com/sshsoft/blog/item/0a02633eaca8d43b70cf6cd9.html
1.标准方法
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title>标准的cookie操作</title>
- <script type="text/javascript">
- //设置cookie名,值,过期时间
- function setCookie(c_name,value,expiredays)
- {
- var exdate=new Date()
- exdate.setDate(exdate.getDate()+expiredays)
- document.cookie=c_name+ "=" +escape(value)+
- ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
- }
- //取得cookie值
- function getCookie(c_name)
- {
- if (document.cookie.length>0)
- {
- c_start=document.cookie.indexOf(c_name + "=")
- if (c_start!=-1)
- {
- c_startc_start=c_start + c_name.length+1
- c_end=document.cookie.indexOf(";",c_start)
- if (c_end==-1) c_end=document.cookie.length
- return unescape(document.cookie.substring(c_start,c_end))
- }
- }
- return ""
- }
- //使用上面两个函数,设置过期时间为一年。
- function checkCookie()
- {
- username=getCookie('username')
- if (username!=null && username!="")
- {alert('Welcome again '+username+'!')}
- else
- {
- username=prompt('Please enter your name:',"")
- if (username!=null && username!="")
- {
- setCookie('username',username,365)
- }
- }
- }
- </script>
- </head>
- <body>
- <input type="button" value="cookie" onclick="checkCookie();" />
- </body>
- </html>
2.猴子的方法
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title>js操作cookie</title>
- <script type="text/javascript">
- //猴子的方法
- /*取出Cookie*/
- function getCookie(name){var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));if(arr!=null)return unescape(arr[2]);return ""}
- /*设置Cookie*/
- function setCookie(name,value){var exp =new Date();exp.setTime(exp.getTime()+30*24*60*60*1000);document.cookie=name+"="+escape(value)+";expires="+ exp.toGMTString()}
- </script>
- </head>
- <body>
- <input type="button" onclick='setCookie("k1","khello")' value="set" />
- <input type="button" onclick='alert(getCookie("k1"))' value="get" />
- </body>
- </html>
3.C#和js相互操作cookie示例
前台
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="js_cs_cookie.aspx.cs" Inherits="js_js_cs_cookie" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>c#和js互操作cookie</title>
- <script type="text/javascript">
- //猴子的方法
- /*取出Cookie*/
- function getCookie(name){var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));if(arr!=null)return unescape(arr[2]);return ""}
- /*设置Cookie*/
- function setCookie(name,value){var exp =new Date();exp.setTime(exp.getTime()+30*24*60*60*1000);document.cookie=name+"="+escape(value)+";expires="+ exp.toGMTString()}
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <input type="button" onclick='setCookie("k1","khello")' value="set" />
- <input type="button" onclick='alert(getCookie("k2"))' value="get" />
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
- </form>
- </body>
- </html>
后台
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class js_js_cs_cookie : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- if (this.TextBox1.Text.Trim() == "get")
- Response.Write(getCookie("k1"));
- else
- setCookie("k2", "khello1");
- }
- /// <summary>
- /// 增加一下Cookie
- /// </summary>
- /// <param name="strName"></param>
- /// <param name="strValue"></param>
- /// <returns></returns>
- public bool setCookie(string strName, string strValue)
- {
- try
- {
- HttpCookie Cookie = new HttpCookie(strName);
- Cookie.Expires = DateTime.Now.AddDays(100);//过期时间设置
- Cookie.Value = System.Web.HttpContext.Current.Server.UrlEncode(strValue);//进入Url编码。
- System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 取得Cookie值,经过Url解码。
- /// </summary>
- /// <param name="strName"></param>
- /// <returns></returns>
- public string getCookie(string strName)
- {
- HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
- if (Cookie != null)
- {
- return System.Web.HttpContext.Current.Server.UrlDecode(Cookie.Value);
- }
- else
- {
- return "无";
- }
- }
- }