Javascript - 未定义的cookie值?

时间:2010-06-10 17:04:23

标签: javascript html

尝试运行代码,我知道问题出在1.部分。 提前谢谢,

P.S。我是JS的新手。

<html>
<head>
<script>
{
    //1. dio

    var Cookies = new Array();

    function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }

    function eraseCookie(name) {
        createCookie(name,"",-1);
    }

    //2. dio

    function saveIt(name) {
        var Cookies = new Array();
        var x = document.forms['forma'].cookievalue.value;
        if (!x)
            alert('Please fill in a value in the input box.');
        else {
            Cookies.create(name,x,7);
            alert('Cookie created');
        }
    }

    function readIt(name) {
        alert('The value of the cookie is ' + Cookies[name]);
    }

    function eraseIt(name) {
        Cookies.erase(name);
        alert('Cookie erased');
    }

    function init() {
        for (var i=1;i<3;i++) {
            var x = Cookies['ppkcookie' + i];
            if (x) alert('Cookie ppkcookie' + i + '\nthat you set on a previous visit, is still active.\nIts value is ' + x);
        }
    }
}
</script>
</head>
<body>
<form name = "forma">
    <input type = "text" name = "cookievalue">
    <input type = "button" value = "Spremi" onClick = "saveIt('ppkcookie1')">
    <input type = "button" value = "Ispisi" onClick = "readIt('ppkcookie1')">   
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

here

Cookies.create似乎不是一个功能

答案 1 :(得分:0)

以下代码我已经隐藏在morebits.js;可能你可以直接使用它:

Cookies = {
    /*
     * Creates an cookie with the name and value pair. expiry is optional or null and defaults
     * to browser standard (in seconds), path is optional and defaults to "/"
     * throws error if the cookie already exists.
     */
    create: function( name, value, max_age, path ) {
        if( Cookies.exists( name ) ) {
            throw "cookie " + name + " already exists";
        }
        Cookies.set( name, value, max_age, path );
    },
    /*
     * Sets an cookie with the name and value pair, overwrites any previous cookie of that name.
     * expiry is optional or null and defaults to browser standard (in seconds),
     * path is optional and defaults to /
     */
    set: function( name, value, max_age, path ) {
        var cookie = name + "=" + encodeURIComponent( value );
        if( max_age ) {
            cookie += "; max-age=" + max_age;
        }
        cookie += "; path=" + path || "/";
        document.cookie = cookie;
    },
    /*
     * Retuns the cookie with the name "name", return null if no cookie found.
     */
    read: function( name ) {
        var cookies = document.cookie.split(";");
        for( var i = 0; i < cookies.length; ++i ) {
            var current = cookies[i];
            current = current.trim();
            if( current.indexOf( name + "=" ) == 0 ) {
                return decodeURIComponent( current.substring( name.length + 1 ) );
            }
        }
        return null;
    },
    /*
     * Returns true if a cookie exists, false otherwise
     */
    exists: function( name ) {
        var re = new RegExp( ";\\s*" + name + "=" );
        return re.test( document.cookie );
    },
    /*
     * Deletes the cookie named "name"
     */
    remove: function( name ) {
        Cookies.set( name, '', -1 );
    }
}