浏览器重启后jquery cookie消失了吗?

Pet*_*ter 6 javascript

我使用https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js作为cookie函数.

我的问题是浏览器重启后cookie似乎被删除了?

以下是代码摘要,

 if ( $.cookie("latlng") ) {
  myLatlng = $.cookie('latlng').split(',');
  myLatlng = new google.maps.LatLng(myLatlng[0], myLatlng[1]);
 } else {
  $.cookie("latlng", "3.139, 101.686", { path: '/' });
  myLatlng = new google.maps.LatLng(3.139, 101.686);
 }

 ...

 google.maps.event.addListener(marker1, 'dragend', function() {
  var temp = marker1.getPosition().lat() + ',' + marker1.getPosition().lng()
  $.cookie("latlng", temp, { path: '/' });
 });
Run Code Online (Sandbox Code Playgroud)

Joe*_*ink 9

添加过期值.在7天后到期:

$.cookie("latlng", "3.139, 101.686", { path: '/', expires:7 })
Run Code Online (Sandbox Code Playgroud)


Bit*_*ter 5

如果您不提供任何选项,$ .cookie("MyCookie","MyValue")将为当前路径级别创建会话cookie.这意味着cookie将在浏览器关闭后过期,并且仅对当前页面可用.通过提供选项参数,如下所示:

$ .cookie("MyCookie","MyValue",{expires:365})

cookie将持续一年.您还可以在选项中包含一个路径,以使Cookie可用于您域中的其他页面,如下所示:

$ .cookie("MyCookie","MyValue",{path:'/',expires:365})

这会创建一个持续一年的cookie,并且可用于您域中的所有页面.(如果您意识到cookie将在每个页面请求上发送到服务器,那么可能不是您想要的,因此请小心使用).