Reputation: 8824
I am using jcookie to store certain information (user cart) and the idea is to re display the contents of the cart when the user visits the website at some other point in future.
So if user adds the item to his cart and i) closes the browser and opens a new browser window after some time OR ii) opens a new tab : In both cases should see the item added to the cart
I am using jcookie.js library. The code I am using to create cookie and add cart contents to it is:
$.cookie('rented_car', $(rentContainer).html());
$.cookie('rented_car_timings', $(divRentalSumm).html());
Also when the page loads, I have in my index.html
<script type="text/javascript">
window.onload=checkCookies;
</script>
where checkcookies() is defined as follows:
function checkCookies(){
var rented_car_timings_cookie = $.cookie("rented_car_timings");
var $rentTimingsContainer = $('<div class="module">' + rented_car_timings_cookie + '</div>');
var rented_car_cookie = $.cookie("rented_car");
var $rentContainer = $('<div class="module">' + rented_car_cookie + '</div>');
if(rented_car_timings_cookie && rented_car_cookie){
$('#rentit').html('');
$('#rentit').append($rentTimingsContainer);
$('#rentit').append($rentContainer);
}
}
Now the problem I am seeing is that the cookies work if I refresh the same page or open a new tab in the same Browser window: I am able to see content added to cart. however, If I open a new window , then I do not see the same . Can anyone please point the problem in my logic?
Upvotes: 0
Views: 1056
Reputation: 8824
$.cookie('rented_car', $(rentContainer).html(),{ expires: 20});
works as this sets expiration time explicitly, default expiration is when browser session closes.
Upvotes: 1