JS操作緩存


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication2.Index" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        //寫入緩存
        function WriteCookie() {
            var date = new Date();
            date.setTime(date.getTime() + 1 * 24 * 3600 * 1000);
            var cookie = "nameJS=nameJSValue2;path =/;expires = " + date;
            document.cookie = cookie;
        }
        //讀取緩存
        function ReadCookie(name) {
            var str = document.cookie.split(";");
            for (var i = 0; i < str.length; i++) {
                var str2 = str[i].split("=");
                if (str2[0] == " nameJS") {//除第一個Cookie外,其它要在前面加上空格 .
                    alert(str2[1]);
                }
            }
        }
        //清除緩存
        function ClearCookie() {
            var dt = new Date();
            dt.setTime(dt.getTime() - 10000);
            var cookie2 = "nameJS = nameJSValue2;path=/;expires = " + dt;
            document.cookie = cookie2;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="div_digg"></div>
    <div>
        <input type="button" οnclick="WriteCookie();" value="寫Cookie" />
        <input type="button" οnclick="ReadCookie('nameJS');" value="讀Cookie" />
        <input type="button" οnclick="ClearCookie();" value="清除Cookie" />
    </div>
    </form>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章