最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to determine if a cookie exists with the jquery cookie plugin? - Stack Overflow

programmeradmin0浏览0评论

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: / 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.

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/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.

Share Improve this question edited Dec 19, 2020 at 9:04 peterh 1 asked May 5, 2011 at 18:53 mattmatt 44.5k107 gold badges268 silver badges402 bronze badges 4
  • 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 Commented May 5, 2011 at 18:56
  • Plugin: plugins.jquery./project/Cookie – Thomas Shields Commented May 5, 2011 at 18:57
  • What are the current cookie limits in modern browsers? – mplungjan Commented May 5, 2011 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 Commented May 5, 2011 at 19:36
Add a ment  | 

1 Answer 1

Reset to default 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();
}
发布评论

评论列表(0)

  1. 暂无评论