JavaScript通过Cookie生成便条纸

2009-06-26 15:27:33 | 【

便条就是一个提示性的小文档,有事件内容,事件的地点等。 如果要用js实现的话,得可以采用Cookie技术。 可以设置内容,和有效时间等属性

<html>
  <head>
       <style type="text/css">
        *{
            font-size:12px;
            font-family:宋体,Arial;
            font-weight:normal;
            color:#333;
        }
        textarea{
            width:100%;
            height:98%;
            border:1px solid black;
        }
    </style>
    
    <script type="text/javascript">

        /*****************************************************
         *  Share JavaScript (http://www.ShareJS.com)
         * 使用此脚本程序,请保留此声明
         * 获取此脚本以及更多的JavaScript程序,请访问 http://www.ShareJS.com
         ******************************************************/
    
        function read_cookie(key){
            var str, ary;
            str = document.cookie;
            ary = str.replace(/ *; */g, ";").split(";");
            key = escape(key) + "=";
            for(var i = 0; i < ary.length; i++){
                if(ary[i].indexOf(key) == 0){
                    return (unescape(ary[i].split("=")[1]));
                }
            }
        }
        
        function write_cookie(key, value,cookieDomain, cookiePath, expireTime,targetWindow){
            //value表示你的时间内容,expireTime表示有效时间
                var strAppendix = "";
            strAppendix += cookieDomain?";domain="+cookieDomain:"";
            strAppendix += cookiePath?";path="+cookiePath:"";
            strAppendix += expireTime?";expires="+expireTime:"";
            targetWindow = targetWindow?targetWindow:top;
            targetWindow.document.cookie = escape(key) + "=" +escape(value)+strAppendix;
        }
        
        function loadDate(){
            if(read_cookie("txt1")){
                $("txt1").value = read_cookie("txt1");
            }
        }
        
        function saveDate(){
            var dt = new Date();
            dt.setDate(dt.getDate()+1);
            write_cookie("txt1",$("txt1").value,false,false,dt.toUTCString());
        }
        
        function $(str){
            return document.getElementById(str);
        }
    </script>
  </head>
  
  <body onload="loadDate();" onunload="saveDate();">
        <textarea id = "txt1">在这里输入内容,关闭页面再次打开,修改后的内容依然存在</textarea>
  </body>
</html>


相关资源