我的漫漫程序之旅

          專注于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 々上善若水々 閱讀(851) 評論(0)  編輯  收藏 所屬分類: JavaScript

          主站蜘蛛池模板: 西和县| 平顺县| 东光县| 海林市| 宕昌县| 太保市| 旌德县| 饶平县| 洞口县| 麦盖提县| 松原市| 铁岭市| 徐州市| 阿勒泰市| 华宁县| 扶绥县| 南靖县| 尚志市| 平遥县| 崇州市| 云和县| 定结县| 慈利县| 含山县| 吉隆县| 高碑店市| 阆中市| 偏关县| 晋江市| 石门县| 大化| 丹江口市| 罗城| 赤峰市| 金湖县| 涟水县| 乌拉特后旗| 建瓯市| 全南县| 平阳县| 武邑县|