c#写入cookie。js获取

基础上的不扎实,对很多计算机名词不是很理解,更不能明白他们的基本原理。所以在用程序C#记录cookie,前端用js读入cookie时,费了我不少劲。终于现在弄懂了怎么回事。好了 直接上代码吧。

ContractedBlock.gif ExpandedBlockStart.gif js
 
   
< script src = " Scripts/jquery-1.2.6.min.js " type = " text/javascript " >< / script>
< script type = " text/javascript " >
function getCookie(name) // 取cookies函数
{
var arr = document.cookie.match( new RegExp( " (^| ) " + name + " =([^;]*)(;|$) " ));
if (arr != null ) return unescape(arr[ 2 ]); return null ;
}
$(
function () {
$(
" #btn " ).click( function () {
$(
" #read " ).val(getCookie( ' username ' ));
})
})


< / script>
ContractedBlock.gif ExpandedBlockStart.gif html
 
   
< form id ="form1" runat ="server" >
< div >
< input type ="text" id ="txt" name ="txts" />< asp:Button ID ="Button1" runat ="server" Text ="写cookie" onclick ="Button1_Click" />< br />
< input type ="text" id ="read" />< input type ="button" id ="btn" value ="读cookie" />
</ div >
</ form >
ContractedBlock.gif ExpandedBlockStart.gif c#
 
   
public static string test;
protected void Page_Load( object sender, EventArgs e)
{

}
protected void Button1_Click( object sender, EventArgs e)
{
test
= HttpContext.Current.Request.Form[ " txts " ];
test
= HttpUtility.UrlEncodeUnicode(test); // 对字符串进行js式escape编码。
HttpCookie txtcookie = new HttpCookie( " username " , test);
DateTime dt
= new DateTime();
dt
= DateTime.Now;
TimeSpan ts
= new TimeSpan( 0 , 0 , 1 , 0 );
txtcookie.Expires
= dt.Add(ts);
Response.SetCookie(txtcookie);
}

转载于:https://www.cnblogs.com/_popc/archive/2011/04/29/2032775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值