当前位置: 代码迷 >> HTML/CSS >> 只用html和javascript实现表单信息的储存和读取解决思路
  详细解决方案

只用html和javascript实现表单信息的储存和读取解决思路

热度:205   发布时间:2012-03-21 13:33:15.0
只用html和javascript实现表单信息的储存和读取
用html编写注册页面后,提交注册信息后,如何将内容保存在cookie里
用html编写登录界面,如何验证 填写的信息是否正确(即 如何从cookie里读取出注册的信息,并加以验证)
我文字功底很低,喜欢大侠们能写的通俗易懂。能写个小小的demo最好不过,先谢谢了 
上一次问了,没人能回答出来,我就不信了,CSDN这么多人就没人能解决?高手都在哪?我会一直等到CSDN倒闭!
 


------解决方案--------------------
HTML code

<!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>
    <title>JS-Cookie testing</title>

    <script type="text/javascript">
        var Cookies = {};
        /** 设置Cookies */
        Cookies.set = function(name, value) {
            var argv = arguments;
            var argc = arguments.length;
            var expires = (argc > 2) ? argv[2] : null;
            var path = (argc > 3) ? argv[3] : '/';
            var domain = (argc > 4) ? argv[4] : null;
            var secure = (argc > 5) ? argv[5] : false;
            document.cookie = name + "=" + escape(value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
        };
        
        /** 读取Cookies */
        Cookies.get = function(name) {
            var arg = name + "=";
            var alen = arg.length;
            var clen = document.cookie.length;
            var i = 0;
            var j = 0;
            while (i < clen) {
                j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                    return Cookies.getCookieVal(j);
                i = document.cookie.indexOf(" ", i) + 1;
                if (i == 0)
                    break;
            }
            return null;
        };
        
        /** 清除Cookies */
        Cookies.clear = function(name) {
            if (Cookies.get(name)) {
                var expdate = new Date();
                expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
                Cookies.set(name, "", expdate);
            }
        };

        Cookies.getCookieVal = function(offset) {
            var endstr = document.cookie.indexOf(";", offset);
            if (endstr == -1) {
                endstr = document.cookie.length;
            }
            return unescape(document.cookie.substring(offset, endstr));
        };
        
        //创建一个Cookie
        Cookies.set('testName', 'testValues');
        //获取该Cookie的值
        alert(Cookies.get('testName'));
        //清除该Cookie
        //Cookies.clear('testName');
        
    </script>

</head>
<body>
</body>
</html> 
  相关解决方案