jQueryを利用しないでJavaScriptでCookieを操作するコードは以下の記事を参照してください。
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
$.cookie([キー名], [値]);
$.cookie([キー名], [値], {[オプションの記述]});
$.cookie('Test', '100', {path: '/' });
$.cookie('Test', '100', { expires: 7});
$.cookie('Test', '100', {domain: 'ipentec.com' });
$.cookie('Test', '100', {secure: true});
$.cookie('Test', '100', { expires: 3, path: '/data' });
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<meta charset="utf-8" />
<script type="text/javascript">
$(document).ready(function () {
$.cookie("Name", "ペンギン", { expires: 14 });
$.cookie("Age", "3", { expires: 14 });
$("#msg").text("Cookieに書き込みました。");
})
</script>
</head>
<body>
<div id="msg"></div>
</body>
</html>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
$.cookie([キー名]);
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript">
$(document).ready(function () {
var valueName = $.cookie("Name");
var valueAge = $.cookie("Age");
$("#msg").html("Cookieの内容(Name): " + valueName + "<br/>" + "Cookieの内容(Age): " + valueAge);
});
</script>
</head>
<body>
<div id="msg"></div>
</body>
</html>
removeCookie()
メソッドを利用します。$.removeCookie([削除するCookieのキー名]);
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript">
$(document).ready(function () {
$.removeCookie("Name");
$.removeCookie("Age");
$("#msg").text("Cookieをクリアしました。");
});
</script>
</head>
<body>
<div id="msg"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript">
$(document).ready(function () {
$.cookie("Name", null);
$.cookie("Age", null);
$("#msg").text("Cookieをクリアしました。");
});
</script>
</head>
<body>
<div id="msg"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="jquery-2.1.4.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript">
$(document).ready(function () {
$.cookie("Name","", {expires: -1});
$.cookie("Age", "", {expires: -1});
$("#msg").text("Cookieをクリアしました。");
});
</script>
</head>
<body>
<div id="msg"></div>
</body>
</html>