尝试运行代码,我知道问题在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>
很难“运行代码”,因为你没有提供实际*电话*任何功能的一部分,你没有为提供HTML形成。 – Pointy 2010-06-10 18:02:18
我做了,它只是没有显示,对不起,我在这里更新它。 – Computeras 2010-06-10 20:40:57
请注意,编辑过程中''消失。您可能需要将其重新放入以避免红鲱鱼。您可能还想复制'n'paste'并运行它来验证是否有任何进展正确。 JS期待一个名称为“cookieform”的表单,但HTML中没有任何一个... – BalusC 2010-06-10 20:50:32