tags:

views:

175

answers:

2

Hi, I am having a little bit of problem deleting a specific cookie. I have created the cookie successfully but the problem is deleting the cookie when a specific del id has been invocked. This is the complete code, can someone please point out what I am missing and if possible, guide me by helping me fix it?

    jQuery(function($){ 

    $("a[id^='savebook-']").click(function(){
    match = this.id.match(/savebook-(\d+)/);
    savedclass = $("span#saved-"+match[1])
    savedclass.addClass("saved");
    var bookid = +match[1];

    var delim = "|"; // delimiter for books in cookie
    // get the previous cookie (if any), split it into an array
    var books = ($.cookie("books_saved") || "").split(delim);
    books.push(bookid);
    $.cookie("books_saved", books.join(delim), { expires: 7, path: '/', domain: 'example.com'});
    return false;
     });


      $("a[id^='delbook-']").click(function(){
    xmatch = this.id.xmatch(/delbook-(\d+)/);
var delim = "|";
    var delid = xmatch[1];
    var books = ($.cookie("books_saved") || "").split(delim);
    var i = null;  
     for (i = 0; books.length > i; i += 1) {  
      if (books[i].delid === delid) {  
                books_saved.slice(delid)  
            }  
     }  

    });





    });
A: 

Without even looking at the code -- there is no guarantee that a cookie will actually be deleted (when triggered by javascript) until the browser closes down. The only way to guarantee cookie deletion cross-browser at a particular point in time is to have a server do it.

Maas
To be clear -- the cookie is scheduled for deletion, but (for example) firefox 3.5 won't stop sending the cookie until it restarts. Go figure.
Maas
This is handled by a jquery plugin, and yes its possible. I have just not figured out whats wrong with my code. A relevant sample is var count = 5; if (books.length > count) { books = books.slice(books.length - count); }Where it removes the last id, this limiting the size of the data in the cookie to 5. I just have not figured out how to fix it. Anybody else please?
kazey
If a browser does not support cookies, it wont be created in the first place.
kazey
A: 

Cookies are deleted by setting them with an ‘expires’ time in the past.

However, I see nothing in this code that's trying to delete a cookie. You seem to be trying to remove part of a string which is set as cookie. (As far as I can tell from the code, which thanks to the crazy indentation is not at all easy to read.)

  if (books[i].delid === delid) {  
            books_saved.slice(delid)  
        }

There's no such variable as ‘books_saved’. You should have got a JavaScript error for this. Make sure you have error reporting/the error console turned on, as without errors you'll be shooting in the dark.

Presumably you meant the ‘books’ array, but that's just strings, not objects with a delid.

.slice returns part of an array as a new array. It doesn't change the array. If you meant to remove an item from the array you want splice, but it wouldn't make any sense to pass a delid to it. Maybe you meant:

for (var i= books.length; i-->0;)
    if (books[i]===delid)
        books_saved.splice(i, 1);

This will remove the given item from the array, but that still won't change the cookie, you have to call $.cookie again afterward just like in the savebook function.

bobince
I solved this using php, thanks anyway :)
kazey