首页  ·  知识 ·  前端
静态页用js设置,获取,删除COOKIE,并显示
佚名  本站原创  综合  编辑:dezai  图片来源:网络
//*********************************新建一测试页面************aspx*********// %@ Page Language="C#" AutoEventWireup="true" CodeFile="ceshi.asp
//*********************************新建一测试页面************aspx*********//

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ceshi.aspx.cs" Inherits="ceshi" %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml" >

    无标题页
   


   
   


   

   

//*********************************为了测试页面********初始cookie用cs文件*************//
protected void Page_Load(object sender, EventArgs e)
    {

       HttpCookie cookie = new HttpCookie("PassportURl");

        string eventUrl = http://www.5ixznr + Request.RawUrl.ToString();

        Session["Url"] = eventUrl.ToString(); //记录当前的地址;
        cookie.Values.Add("5ixznr", eventUrl.ToString()); //记录当前的地址;

        HttpContext.Current.Response.Cookies.Remove("PassportURl"); //删除旧的同名Cookie
        Response.AppendCookie(cookie);

}

//*********************************cookie.js函数*************//

// JScript 文件

function GetCookieVal(offset)
            //获得Cookie解码后的值
            {
            var endstr = documents.cookie.indexOf (";", offset);
            if (endstr == -1)
            endstr = documents.cookie.length;

              var msg=document.getElementById("emailInfor");               
               msg.innerHTML="Wellcome  "+documents.cookie.substring(unescape(documents.cookie.substring(offset, endstr)).indexOf("=")+13);
            //return unescape(documents.cookie.substring(offset, endstr));
            }
            function SetCookie(name, value)
            //设定Cookie值
            {
            var expdate = new Date();
            var argv = SetCookie.arguments;
            var argc = SetCookie.arguments.length;
            var expires = (argc > 2) ? argv[2] : null;
            var path = (argc > 3) ? argv[3] : null;
            var domain = (argc > 4) ? argv[4] : null;
            var secure = (argc > 5) ? argv[5] : false;
            if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
            documents.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
            +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
            +((secure == true) ? "; secure" : "");
            }
            function DelCookie(name)
            //删除Cookie
            {
            var exp = new Date();
            exp.setTime (exp.getTime() - 1);
            var cval = GetCookie (name);
            documents.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
            }
            function GetCookie(name)
            //获得Cookie的原始值
            {
            var arg = name + "=";
            var alen = arg.length;
            var clen = documents.cookie.length;
            var i = 0;
            while (i < clen)
            {
            var j = i + alen;
            if (documents.cookie.substring(i, j) == arg)
            return GetCookieVal (j);
            i = documents.cookie.indexOf(" ", i) + 1;
            if (i == 0) break;
            }
             var msg=document.getElementById("emailInfor");
               msg.innerHTML="取到为空";
            //return null;
            }

本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读