静态页动态读写Cookie

首页登录  代码如下

//存入cookie  
            HttpCookie newCookie = new HttpCookie("User");  
            newCookie.Expires.AddMinutes(30);//设置时间  
            newCookie.Values.Add("uid","001");  
            newCookie.Values.Add("username","leo li");  
            Response.AppendCookie(newCookie); 

html   代码如下

<html>  
<title>测试</title>  
<mce:script type="text/javascript" language="javascript"><!--  
    function GetCookie(sMainName, sSubName) {  
        var sCookieName = sMainName + "=";  
        var sSubCookieName = (sSubName) ? sSubName + "=" : null;  
        var sCookie;  
        var sWholeCookie = document.cookie;  
  
        var nValueBegin = sWholeCookie.indexOf(sCookieName);  
        if (nValueBegin != -1) {  
            var nValueEnd = sWholeCookie.indexOf(";", nValueBegin);  
            if (nValueEnd == -1)  
                nValueEnd = sWholeCookie.length;  
  
            var sValue = sWholeCookie.substring(nValueBegin + sCookieName.length, nValueEnd); //获得Cookie值  
  
            if (sSubCookieName)//多值Cookie  
            {  
                var nSubValueBegin = sValue.indexOf(sSubCookieName);  
                if (nSubValueBegin != -1) {  
                    var nSubValueEnd = sValue.indexOf("&", nSubValueBegin);  
                    if (nSubValueEnd == -1)  
                        nSubValueEnd = sValue.length;  
                    var sSubValue = sValue.substring(nSubValueBegin + sSubCookieName.length, nSubValueEnd); //获得指定的子键值  
                    return unescape(sSubValue);  
                }  
            }  
            if (!sSubCookieName)  
                return unescape(sValue);  
        }  
        return null;  
    }  
    function a() {  
        if (GetCookie('User', 'name') != null) {  
            document.write('用户名:' + GetCookie('User', 'name') + '<a href="/logout.aspx" mce_href="logout.aspx">注销</a>');  
        }  
        else {  
            document.write('<a href="/login.aspx" mce_href="login.aspx">登录</a> <a href="/reg.aspx" mce_href="reg.aspx">注册</a>');  
        }  
    }  
// --></mce:script>  
<head>  
</head>  
<body onload="a()">  
</body>  
</html>  

转载至 : http://blog.csdn.net/lilin8905/article/details/5568327

原文地址:https://www.cnblogs.com/yudishow/p/4288387.html