クッキーの削除方法。

[新着] Webテンプレートを仮オープンしました



0   名前: ふじこ : 2006/09/07(木) 10:20  ID:mDpRXot9
JAVAスクリプト初心者です。

以下のように、クッキーを使った入力補助フォームに
クッキーの削除をできるようにしたいのですが、
その場合、どのようにすればよいのでしょうか。。
だれか、助けてください。








<html>


<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta name="keywords"content="">

<script type="text/javascript">
<!--
//ユーザが設定する変数----------------------------------
var tId = new Array("name", "mail", "textarea");
var formNum = 0;
//------------------------------------------------------
//tId配列には対象とする要素のIDを
//formNumには<form>の番号(0から数える)を
//設定してください
//------------------------------------------------------
var count = 0;
function writeCookie(cName, cBody){
//クッキーに書き込み
temporary = cName + "=" + escape(cBody) + ";"
document.cookie = temporary + "expires=Fri, 31-Dec-2050 23:59:59 GMT;";
return true;
}
function readCookie(word){
value = document.cookie + ";";
pos = value.indexOf(word, 0);
if (pos != -1){
startFlag = value.indexOf("=", pos) + 1;
endFlag = value.indexOf(";", pos);
string = value.substring(startFlag, endFlag);
string = unescape(string);
return string;
}
else {
return "";
}
}
function setCookie(){
if (document.getElementById){
while (count < tId.length){
//登録されたテキストボックスの内容を記録
tValue = document.getElementById(tId[count]).getAttribute("value");
writeCookie(tId[count], tValue);
count ++;
}
}
count = 0;
return true;
}
function getCookie(){
if (document.getElementById){
while (count < tId.length){
//クッキーの内容をページに反映
tValue = readCookie(tId[count]);
document.getElementById(tId[count]).setAttribute("value", tValue);
count ++;
}
}
count = 0;
return true;
}
window.onload = getCookie;
-->
</script>
<body bgcolor="#EEEEEE">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<form method="POST" action="mailto:aaaa@ccccccc" onSubmit="setCookie()">
<b style="width:100px">おなまえ</b>:
<input type="text" size="40" name="name" id="name" value=>
<br>
<b style="width:100px">メール</b>:
<input type="text" size="40" name="mail" id="mail" value=>
<br>
<span style="float:left"><b style="width:100px;">メッセージ</b>:</span>
<textarea rows="10" cols="40" name="textarea" id="textarea" value=></textarea>
<br>
<br>
<input type="submit" value="送信" name="submit">
</form>
</body>
</html>



1   名前: sevi- : 2006/09/07(木) 10:20  ID:9J5RKOHs
クッキーの設定項目の中の
expires=値
の値を,現在時間以前にして設定すれば消えるぞ.

2   名前: ふじこ : 2006/09/07(木) 10:20  ID:mDpRXot9
早速のご教授ありがとうございます。
少々説明がたりなくてすみません。

<INPUT type="button" value="クッキーの削除" onClick="deleteCookie()">
といったボタンを押すと、消えるようにしたいな。。と思っています。

これは可能でしょうか?

3   名前: sevi- : 2006/09/07(木) 10:20  ID:PQukhZTw
いや,ちゃんと説明したつもりなんだが(汗
どうも理解してないようなのでもう少し詳しく言うと,

まずwriteCookie関数を有効期限が指定できるように修正し,

function writeCookie(cName, cBody, dtExpires)
{
	if(dtExpires == null)
		dtExpires = new Date(2050, 12, 31, 23, 59, 59);
	//クッキーに書き込み
	temporary = cName + "=" + escape(cBody) + ";" + "expires=" + dtExpires.toGMTString() + ";";
	document.cookie = temporary;// + "expires=Fri, 31-Dec-2050 23:59:59 GMT;";
	return true;
}

次にページ全体の必要な項目を一括でクッキーに書き込むsetCookie関数にも有効期限の指定が可能なよう修正を施し,

function setCookie(dtExpires)
{
	if (document.getElementById)
	{
		while (count < tId.length)
		{
			//登録されたテキストボックスの内容を記録
			tValue = document.getElementById(tId[count]).getAttribute("value");
			writeCookie(tId[count], tValue, dtExpires);
			count ++;
		}
	}
	count = 0;
	return true;
}

後はdeleteCookie関数を新規作成し,その中で現在時刻より1ミリ秒古い有効期限を引数にsetCookieを呼び出せば良い.

function deleteCookie()
{
	var dt = new Date();
	dt.setTime(dt.getTime()-1);
	setCookie(dt);
}

4   名前: ふじこ : 2006/09/07(木) 10:20  ID:mDpRXot9
ありがとうございました〜!!!
嬉しすぎて、叫んでしまいました。

毎日ソースとにらめっこしていて、どうしようも途方にくれていたので
とても嬉しいです。

レベルの低い質問にも丁寧に回答くださり、本当にありがとうございます!!!

一覧へ戻る