自己做过的项目中的两段代码,你拿去改一下吧。
//获取cookie
function GetCookie(sName)
{
// cookies are separated by semicolons
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
// a name/value pair (a crumb) is separated by an equal sign
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}
// a cookie with the requested name does not exist
return "";
}
function $(id)
{
return document.getElementById(id);
}function setCookies()
{

  try {
    o=$("province");
if(o.options[o.selectedIndex])
{
var PV=o.options[o.selectedIndex].value;
}

o=$("city");
if(o.options[o.selectedIndex])
{
var CV=o.options[o.selectedIndex].value;
}

var Then = new Date(); 
Then.setTime(Then.getTime() + 60*1000*60*24*365 ); ///一年
document.cookie = "COOKIES_city="+CV+";expires="+ Then.toGMTString();
document.cookie = "COOKIES_province="+PV+";expires="+ Then.toGMTString();
  }
  catch(e) {
    
  }


}

解决方案 »

  1.   

     #region 退出登陆
     protected void LogoffSystem()
     {
        HttpCookie myCookie = new HttpCookie("HISCookie");
        myCookie.Expires = DateTime.Now.AddDays(-1d);
        Response.Cookies.Add(myCookie);
        Response.Redirect("~/Login.aspx");
     }
     #endregion
      

  2.   

    給你一個測試用的例子,具體功能你自己搞定
    <html>
    <head>
    <title>Cookies</title><script src="lib_cookie.js">
    </script></head><body>
    <h2>Set Cookie</h2>
    <form name="setcooky">
    Name:  <input type="text" size="30" name="setcooky_name">
    Value: <input type="text" size="30" name="setcooky_value">
    <input type=button value="Set Cookie" 
    onclick="set_cookie(setcooky_name.value, setcooky_value.value);
    alert('The cookie \'' + setcooky_name.value + '\', with value \'' + setcooky_value.value + '\', has been set.')"
    </form><hr><h2>Get Cookie</h2>
    <form name="getcooky">
    <ul>
    <li>Name:  <input type="text" size="30" name="getcooky_name">
    <input type=button value="Get Cookie Value" 
    onclick="getcooky_value.value=get_cookie(getcooky_name.value)">
    Value: <input type="text" size="30" name="getcooky_value">
    <br><br>
    <li><input type=button value="Get document.cookie" onClick="alert(document.cookie)">
    </ul>
    </form><hr><h2>Delete Cookie</h2>
    <form name="delcooky">
    Name:  <input type="text" size="30" name="delcooky_name">
    <input type=button value="Delete Cookie" 
    onclick="del_cookie(delcooky_name.value);
    alert('The cookie \'' + delcooky_name.value + '\' has been deleted.')"
    </form>
    </body>
    </html>
    <!-- ads begin -->
    </noscript>
    <!-- -->
    </noscript>
    <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
    <!--
    // set expiration to 3 minutes
    var expiration = new Date(); expiration.setTime(expiration.getTime() + 180000);function getCookie(name){
      var cname = name + "=";
      var dc = document.cookie;
      if (dc.length != 0) {
        begin = dc.indexOf(cname);
        if (begin != -1) {
          begin += cname.length;
          end = dc.indexOf(";", begin);
          if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        }
      }
      return null;
    }function setCookie(name, value, expires) {
      document.cookie = name + "=" + escape(value) + 
      ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
      "; path=/";
    }
    var xtpop;function popWin() {
    setCookie('Visits', 1, expiration);
    xtpop = window.open("http://clickit.go2net.com/adclick?cid=197064&area=va.dir.edu&site=va&shape=exitpopup&keyword=exitpopup","xtpop","width=700,height=560,toolbar,scrollbars,menubar,resizable,top=100,left=100");
    xtpop.blur();
    setTimeout("xtpop.blur();",20);
    setTimeout("xtpop.blur();",100);
    self.focus();
    }var visitsCookie = getCookie('Visits');
    if (visitsCookie == null) popWin();// --></SCRIPT>
    <SCRIPT LANGUAGE="JavaScript"><!--
    browser = (((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 2 )) || ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 2 )));
    browser4 = (((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4 )) || ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 )));
    if (browser4) {
        if (!self.url) {
           self.url = '';
        }
        if (parent.name != 'test') {
           test = window.open("http://adincl.gopher.com/adpopup?site=VA&border=1&area=DIR.EDU&sizerepopup=1", "test", "resizable=yes,width=528,height=200,scrollbars");
        }
    } else if (browser) {
        if (!self.url) {
           self.url = '';
        }
        if (parent.name != 'test') {
           test = window.open("http://adincl.gopher.com/adpopup?site=VA&shape=banner&border=1&area=DIR.EDU", "test", "resizable=yes,width=528,height=200,scrollbars");
        }
    }//--></SCRIPT>
    <!-- ads end -->