简体   繁体   中英

How to determine if a cookie exists with the jquery cookie plugin?

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM