都要引用jquery和jquery.cookies.js
<script src="js/jquery.js"></script>
<script src="js/cookies.js"></script>
A页面
console.log($.cookie('the_cookie'));//打印看一下
<button id="aa">保存cookies</button>//设置保存cookie
$("#aa").on('click',function(){
$.cookie('the_cookie', '123456789', { expires: 7, path: '/' });
location.href='b.html';
})
B页面
<button id="bb">清除cookies</button>//清除A页面的cookie
console.log($.cookie('the_cookie'));
$("#bb").on('click',function(){
$.cookie('the_cookie', null, { path: '/' });
console.log($.cookie('the_cookie'));
location.href='a.html';
})
cookie有名字和值,还有域(domain),过期时间(expires),路径(path)等属性。 其中不同的域、不同的路径下可以存在同样名字的cookie。
所以,最好的方式是设置Cookie的时候怎么写的,删除的时候就怎么写。