Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery cookie disappear after browser restart?

Tags:

javascript

I using https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js for the cookie function.

My problem is that the cookie seems like deleted after browser restart?

Here are the summary on the code,

 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: '/' });
 });
like image 474
Peter Avatar asked Jan 30 '11 07:01

Peter


2 Answers

add an expires value. to expire in 7 days:

$.cookie("latlng", "3.139, 101.686", { path: '/', expires:7 })
like image 165
Joe Hanink Avatar answered Oct 31 '22 01:10

Joe Hanink


If you don't supply any options, $.cookie("MyCookie","MyValue") will create a session cookie for the current path level. That means that the cookie will expire after the browser is closed and will only be available for the current page. By supplyng the options parameter like this:

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

the cookie will last for a year. You can also include a path in the options to make the cookie available to other pages on you domain like this:

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

whichs creates a cookie that'll last a year and available to all pages on your domain. (which might not what you want if you realize that the cookie will be sent to the server on every page request so use with care).

like image 20
Bitsplitter Avatar answered Oct 31 '22 00:10

Bitsplitter