2010-06-10 54 views
0

尝试运行代码,我知道问题在1.部分。 在此先感谢,Javascript - 未定义的cookie值?

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> 
+0

很难“运行代码”,因为你没有提供实际*电话*任何功能的一部分,你没有为提供HTML形成。 – Pointy 2010-06-10 18:02:18

+0

我做了,它只是没有显示,对不起,我在这里更新它。 – Computeras 2010-06-10 20:40:57

+0

请注意,编辑过程中''消失。您可能需要将其重新放入以避免红鲱鱼。您可能还想复制'n'paste'并运行它来验证是否有任何进展正确。 JS期待一个名称为“cookieform”的表单,但HTML中没有任何一个... – BalusC 2010-06-10 20:50:32

回答

0

here

Cookies.create似乎并没有成为一个功能

+0

Cookies是一个数组,createCookie是一个函数,我试图将其更改为Cookies.createCookie,但它没有做任何事情? – Computeras 2010-06-10 21:05:17

+0

@Computeras - 这不是JS的工作原理。一方面,你有一个名为FooBar的函数,并且你正在调用一个名为Foo的函数(不同的名称=不同的对象在这里),你需要为一个对象分配一个函数来使用点符号('Cookies.create = createCookie; '会在这里完成),最后你的saveIt函数会在范围内创建一个新的变量,它的名字永远不会起作用。我强烈建议你使用一些现有的cookie操作代码(azatoth的一样好,并用它来帮助你了解更多关于JS)的副本。 – annakata 2010-06-11 06:20:33

+0

反正我不太了解JS,所以我只能部分地理解你刚才所说的。所以没关系。如果我再次需要cookies的代码,我会使用现有的代码,就像你说的。无论如何感谢你的努力 – Computeras 2010-06-12 16:37:50

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); 
    } 
}