4

I can save a cookie like this $.cookie('position' + $(this).index('li').toString(), currentTop.toString());

And I'm able to delete a cookie with this $.cookie('position' + $(this).index('li').toString(), null);

However how can I query if one of those cookies exists?

if ( $.cookie('position'+ $(this).index('li').toString()) == null ) {

thank you

update/edit:

I have horizontal list-elements (absolute position and 100% width) that are draggable. When dragged (and dropped) I want to save the position of each list-element with a cookie so the site remembers the position of each element.

<ul class="bars">  
    <li><a href="home">Some Name</a></li>  
    <li class="page_item"><a href="#" title="Downloads">Downloads</a></li>  
    <li class="page_item"><a href="#" title="Contact">Contact</a></li>  
    <li class="page_item"><a href="#" title="Work">Work</a></li> 
</ul>  

Example: http://jsfiddle.net/wa9Ga/ As explained above I want to store each item's position with a cookie. And now to my question above - I want to distribute each list-item randomly if the visitor is visiting the website for the first time (so no cookie has been set).

How can I solve this.

peterh
  • 11,875
  • 18
  • 85
  • 108
matt
  • 42,713
  • 103
  • 264
  • 397
  • Unrelated, browsers have limits in allowed amount of cookies per domain. Although this is usually pretty high (~255), you'd rather like to store the strings as cookie value instead, eventually as an JS array/object. This makes traversion/manipulation also easier. – BalusC May 05 '11 at 18:56
  • Plugin: http://plugins.jquery.com/project/Cookie – Thomas Shields May 05 '11 at 18:57
  • [What are the current cookie limits in modern browsers?](http://stackoverflow.com/questions/5381526/what-are-the-current-cookie-limits-in-modern-browsers) – mplungjan May 05 '11 at 19:24
  • Thank you, but how do I store this stuff as an array? I simply wanna store the y-position of multiple draggable list-items with a cookie so the page remembers the position of the elements. I updated my question? – matt May 05 '11 at 19:36

1 Answers1

5

if you want to test if a cookie exists you can simply do this:

var cookie_name = 'position'+ $(this).index('li').toString();

if ($.cookie(cookie_name)) {
    do_something();
}
else {
    do_something_else();
}
user716340
  • 81
  • 2