jQuery cookie does not set in this script
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
开发者_Go百科 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
精彩评论