这个程序的执行目的是,把信息添加入cookie并可以通过js从cookie中读取出信息并以html的方式呈现,本程序在Firefox3.0测试通过,但是在IE 7.0和Chrome 0.2.149.30中测试出错,请问是哪里出错了,怎样修复呢,谢谢大家。
cookies.js://写cookies函数 作者:翟振凯
function setCookie(name, value)//两个参数,一个是cookie的名子,一个是值
{
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}html:<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- author b 2008-9-20 offline stor use javascript and cookie and test pass in firefox 3.0 but error in IE 7 and Chrome 0.2.149.30 -->
<!-- e-mail [email protected] -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Offline store use javascript and cookie</title>
<script src="cookie.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    // to list contant button list in div1
    function listContant() {
        var NumCookie = getCookie("NumCookie"); // get current contant cookie number
        var CookieContant = new Array(); // temp store the contant list in this array
        if (NumCookie == null) {
            // if first run this page than set the number of contant to zero for init
            NumCookie = 0;
            setCookie("NumCookie", NumCookie.toString());
        } else {
            // load contant to array
            NumCookie = parseInt(NumCookie);
            for (var i = 1; i <= NumCookie; i++) {
                CookieContant.push(getCookie("Contant" + i));
            }
        }
        // debug info
        //alert(NumCookie);
        //alert(CookieContant.join(" ; "));
        // clear contant button list ready to refresh display
        document.getElementById("div1").innerHTML = "";
        // one button represent one contant
        for (var j = 0; j < CookieContant.length; j++) {
            var li = document.createElement("input");
            li.type = "button";
            if (document.attachEvent)
                // for IE
                li.attachEvent("onclick", clickButton2);
            else
                // for Firefox
                li.addEventListener("click", clickButton2, false);
            li.value = "Contant" + (j + 1);
            // append button in div1
            document.getElementById("div1").appendChild(li);
        }
    }    // store contant to cookie
    function clickButton1() {
        var NumCookie = getCookie("NumCookie");
        NumCookie = parseInt(NumCookie) + 1;
        setCookie("NumCookie", NumCookie.toString()); // refresh number of contant to cookie
        setCookie("Contant" + NumCookie, document.getElementById("Textarea1").value);
        listContant(); // after insert a contant then refresh button list
    }    // display cookie to contant, button click event handler
    function clickButton2() {
        var contant = this.value;
        //alert(this.value);
        document.getElementById("div2").innerHTML = getCookie(contant);
        //alert(getCookie(contant));
    }
</script>
</head>
<body onload="listContant()">
<form id="form1" action="#">
<label for="Textarea1">new contant</label><br />
<textarea cols="50" rows="10" id="Textarea1" name="Textarea1"></textarea><br />
<input type="button" id="button1" name="button1" value="Store" onclick="clickButton1()" /><br />
<label for="div1">contant list</label><br />
<div id ="div1">
</div><br />
<label for="div2">current contant</label><br />
<div id ="div2"></div><br />
</form>
</body>
</html>

解决方案 »

  1.   

    function Cg(n){ //读取cookie
    var c=document.cookie.match(new RegExp("(^| )"+n+"=([^;]*)(;|$)"));
    if(c==null){
    return false;
    }else{
    return c[2];
    }
    }
    function Cs(n,v,s){ //存储cookie
    if(!s)var s=86400000000;
    var d=new Date();
    d.setTime(d.getTime()+s);
    document.cookie=n+"="+v+";expires="+d.toGMTString();
    }//我用的 IE7.0,IE8.0 b2,Chrome, Firefox都没有出错
      

  2.   

    操作cookie没有出错,但是在我把输入的保存好信息,并读取信息后显示出来在IE和CHROME上就出错了,
                if (document.attachEvent)
                    // for IE
                    li.attachEvent("onclick", clickButton2);
                else
                    // for Firefox
                    li.addEventListener("click", clickButton2, false);
    好像在IE和CHROME上有些不同,当按下BUTTON调用clickButton2函数时,var contant = this.value;
    中的this得不到调用事件的对象。