我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

JavaScript全方位操作Cookie

//清除Cookies
    function clearCookie()
    
{
        WriteCookie(
"username",'');
        WriteCookie(
"password",'');
        document.getElementById(
"cookie_info").innerHTML = "清除成功";
    }

    
//写cookie
    function WriteCookie (cookieName, cookieValue, expiry) 
    
{
        
var expDate = new Date();
        
if(expiry) //如果设置了cookie失效时间;
        {
            expDate.setTime (expDate.getTime() 
+ expiry);
            document.cookie 
= cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString();
        }

        
else //没有设置cookie失效时间;
        {
            document.cookie 
= cookieName + "=" + escape (cookieValue);
        }

    }

    
//写cookie
    function setCookie()
    
{
        
var username = document.getElementById("username").value;
        
var password = document.getElementById("password").value;
        
var num = document.getElementById("cookieDate").value;
        
if(num == 0)
        
{
            WriteCookie(
"username", username, 0);
            WriteCookie(
"password", password, 0);
        }

        
else if(num == 1)
        
//如果选择的是天;时间换算成秒;
        {
            WriteCookie(
"username", username, 1000 * 60 * 60 * 24 * num);
            WriteCookie(
"password", password, 1000 * 60 * 60 * 24 * num);
        }

        
else if(num == 2)
        
//如果选择的是月;
        {
            WriteCookie(
"username", username, 1000 * 60 * 60 * 24 * num * 31);
            WriteCookie(
"password", password, 1000 * 60 * 60 * 24 * num * 31);
        }

        
else if(num == 3)
        
//如果选择的是年;
        {
            WriteCookie(
"username", username, 1000 * 60 * 60 * 24 * num * 365);
            WriteCookie(
"password", password, 1000 * 60 * 60 * 24 * num * 365);
        }

    }

    
    
    
//读cookie的值 
    function ReadCookie (CookieName)
    
{
        
var CookieString = document.cookie;
        
var CookieSet = CookieString.split (';');
        
var SetSize = CookieSet.length;
        
var CookiePieces
        
var ReturnValue = "";
        
var x = 0
        
for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++
        
{
            CookiePieces 
= CookieSet[x].split ('=');
            
if (CookiePieces[0].substring (0,1== ' ') 
            
{
                CookiePieces[
0= CookiePieces[0].substring (1, CookiePieces[0].length);
            }

            
if (CookiePieces[0== CookieName) 
            
{
                ReturnValue 
= CookiePieces[1];
            }

        }

        alert (
"Cookie Value is:"+ReturnValue);
    }
form:
<form action="${ctx}/user.do?method=login" method="post" onsubmit="return check();">
                
<table cellpadding=0 cellspacing=0 border=0 width=98%
                    
bgcolor=#0099cc align=center>
                    
<tr>
                        
<td>
                            
<table cellpadding=6 cellspacing=1 border=0 width=100%>

                                
<tr>
                                    
<td bgcolor=#99ccff valign=middle colspan=2 align=center>
                                        
<!-- 判断用户名或密码是否错误 -->
                                        
<logic:empty name="error" scope="request">
                                            
<font color="green"><b>请输入您的用户名、密码登陆</b></font>
                                        
</logic:empty>
                                        
<logic:notEmpty name="error" scope="request">
                                            
<bean:message key="login.error"/>
                                        
</logic:notEmpty>
                                    
</td>
                                
</tr>
                                
<tr>
                                    
<td bgcolor=#f2f8ff valign=middle>
                                        请输入您的用户名
                                    
</td>
                                    
<td bgcolor=#f2f8ff valign=middle>
                                        
<INPUT value="${requestScope.username}" name="username" type=text id="username" onblur="return check();">
                                        
&nbsp;
                                        
<href="${ctx}/main/common/reg.jsp">没有注册?</a>
                                    
</td>
                                    
<!-- 用户名错误提示信息 -->
                                    
<td id="username_info" style="color:red;position: absolute;left:550px;top:235px;"></td>
                                
</tr>
                                
<tr>
                                    
<td bgcolor=#f2f8ff valign=middle>
                                        请输入您的密码
                                    
</td>
                                    
<td bgcolor=#f2f8ff valign=middle>
                                        
<INPUT value="${requestScope.password}" name="password" type=password id="password" onblur="return check();">
                                        
&nbsp;
                                        
<href="lostpass.jsp">忘记密码?</a>
                                    
</td>
                                    
<!-- 密码错误提示信息 -->
                                    
<td id="password_info" style="color:red;position: absolute;left:550px;top:270px;"></td>
                                
</tr>
                                
<tr>
                                    
<td bgcolor=#f2f8ff valign=top width=30%>
                                        
<b>Cookie 选项</b>
                                        
<BR>
                                        请选择您的Cookie保存时间
<br>
                                        下次访问可以方便输入
<br><br />
                                        
<href="#" onclick="clearCookie();" style="color:green;">清空Cookies</a>
                                        
<div id="cookie_info" style="color:maroon;position: absolute;left:100px;top:360px;"></div>
                                    
</td>
                                    
<td bgcolor=#f2f8ff valign=middle>
                                        
<input type="radio" id="cookieDate" name="cookieDate" value="0" checked>
                                        不保存,关闭浏览器就失效
                                        
<br>
                                        
<input type="radio" id="cookieDate"  name="cookieDate" value="1">
                                        保存一天
                                        
<br>
                                        
<input type="radio" id="cookieDate"  name="cookieDate" value="2">
                                        保存一月
                                        
<br>
                                        
<input type="radio" id="cookieDate" name="cookieDate" value="3">
                                        保存一年
                                        
<br>
                                    
</td>
                                
</tr>
                                
<input type=hidden name=comeURL
                                    
value="#" />
                                
<tr>
                                    
<td bgcolor=#99ccff valign=middle colspan=2 align=center>
                                        
<input type=submit name="submit"  onclick="setCookie();" value="登 陆">
                                    
</td>
                                
</tr>
                            
</table>
                        
</td>
                    
</tr>
                
</table>
            
</form>


posted on 2008-01-23 18:19 々上善若水々 阅读(831) 评论(0)  编辑  收藏 所属分类: JavaScript


只有注册用户登录后才能发表评论。


网站导航: