1

I'm trying to extend this script to allow working with multiple pages.

Instead of using a single timestamp I want to use a comma-separated list like this: 1305536229613,1305536315210,1305536318026

The problem is the script server side sets cookies correctly but js code does not work (at least in Firefox 3.6.17 on Opensuse 11.3).

I had to add $.cookie(cookieName, null) (!) before setting time interval to get cookie = $.cookie(cookieName); to work 2 lines below... (how did I found it I don't know...).

Moreover updating the cookie does not work: I can see in another page using a server script cookie always growing

$(document).ready(function(){
    var cookieCheckTimer;
    var cookieName = 'download_token';
    var $wait = $('.wait');
    var $waitMsg = $('<span>Processing, please wait...</span>').prepend(
        $('<img />').attr('src', '/img/wait.gif').css({
            'vertical-align': 'middle',
            'margin-right': '1em'
        }));
    //add hidden field to forms with .wait submit
    $wait.parent().each(function(){
        $(this).append($('<input />').attr({
            'type': 'hidden',
            'name': cookieName,
            'id': cookieName
        }));
    });
    $wait.click(function(){
        var token = String(new Date().getTime());
        //set token value
        $('#' + cookieName).val(token);
        //hide submit buttons
        $(':submit').hide();
        //append wait msg
        $(this).after($waitMsg);
        //why?? don't know...
        $.cookie(cookieName, null) //<<<<<<<<<<
        //set interval
        cookieCheckTimer = window.setInterval(function(){
            var cookie = $.cookie(cookieName);
            if (cookie != null){
                cookie = cookie.split(',');
                var idx = cookie.indexOf(token);
                if (idx >= 0){
                    //detach wait msg
                    $waitMsg.detach();
                    //show again submit buttons
                    $(':submit').show();
                    //remove token
                    cookie.splice(idx, 1);
                    //clear timer
                    window.clearInterval(cookieCheckTimer);
                    //update cookie value
                    if (cookie) $.cookie(cookieName, cookie.join(','));
                    else $.cookie(cookieName, null);
                }
            }
        }, 1000);
    });
});

I'm using latest version of the jquery.cookie plugin from repository

Community
  • 1
  • 1
neurino
  • 11,500
  • 2
  • 40
  • 63
  • Do you get any errors in your console? Specifically what part of the javascript isn't working? – Gary Green May 16 '11 at 09:40
  • don't use != null because what if the cookie is empty or undefinied your .split wont work or splice (at least it wont work cross browser) moreover you can use arrayed cookie, $.cookie('user[]'); or something like that :) easy to manage aswell :) – Val May 16 '11 at 09:43
  • @Gary: no errors in console, if I remove the line marked with `<<<<` comment `$.cookie(cookieName)` does not update, last part commented with `//update cookie value` does not updates anything. The script is tied to a submit button that starts a file download. Thanks – neurino May 16 '11 at 09:44
  • @Val: the script should handle `"".split[',']` properly (simply it won't find the token) and $.cookie never returns `undefined` but `null`. Anyway, can you post an example of arrayed cookie? – neurino May 16 '11 at 09:50
  • gimmie a link of ur plugin cookie e.g. then I can use it on jsfiddle – Val May 16 '11 at 09:52
  • I was trying to mess up with it here: http://jsfiddle.net/P7KKU/ but don't know how to set cookies outside the page... – neurino May 16 '11 at 09:56
  • http://jsfiddle.net/P7KKU/6/ which means you do not have the cookie plugin attached, which meant $.cookie function does not exist, – Val May 16 '11 at 10:09
  • @Val, sorry, I corrected it already, I thought it was updated... anyway here's a [php version](http://neurino.altervista.org/cookies.php) I'm trying to reproduce the problem – neurino May 16 '11 at 10:18
  • http://jsfiddle.net/zCHmd/ should help also this question here is very good to help you get a better idead of it, http://stackoverflow.com/questions/1959455/how-to-store-an-array-in-jquery-cookie ... Lastly Your token is not inside the interval which means it will not be renewed :) – Val May 16 '11 at 10:42
  • thanks for the link, I cannot reproduce error in php so it must be something with server side – neurino May 16 '11 at 11:11

0 Answers0