开发者

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

开发者 https://www.devze.com 2023-03-03 10:16 出处:网络
I can save a cookie like this $.cookie(\'position\' + $(this).index(\'li\').toString(), currentTop.toString());

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">开发者_Python百科<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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消