Multiple Cookie Generation Issue
Posted
by Shannon
on Stack Overflow
See other posts from Stack Overflow
or by Shannon
Published on 2010-05-10T13:50:35Z
Indexed on
2010/05/10
13:54 UTC
Read the original article
Hit count: 337
Hi all,
jQuery newbie here. I need to be able to set multiple cookies within the code without have to change out this variable each and every time. Is there any way to make this code generate unique cookies for different pages? As it is now, I'm having to rename that variable for each page that the jQuery animations exist on. (sbbcookiename)
Background on the issue: We are having issues with the sliders not autoplaying once one has already been triggered, due to it the cookie having been cached.
Thanks for your help.
(function(){
jQuery.noConflict();
var
_TIMEOUT= 1000,
initTimer= 0,
sbLoaded= false,
_re= null
;
initTimer= setTimeout(initSlider, _TIMEOUT);
jQuery(document).ready(initSlider);
function initSlider(){
if(sbLoaded) return;
if (jQuery('#campaign_name').length > 0) {
var sbbcookiename = jQuery('#campaign_name').attr('class');
} else {
var sbbcookiename = "slider728x90";
}
var
slideTimeout //timer
,sbTrigger = jQuery('#slidebartrigger') //convenience
,sbFirstSlide = (document.cookie.indexOf(sbbcookiename) == -1) //check cookie for 'already seen today'
;
clearTimeout(initTimer);
sbLoaded= true;
function toggleSlideboxes(){
if(slideTimeout) clearTimeout(slideTimeout);
var isDown = sbTrigger.is('.closeSlide');
jQuery('#slidebar')['slide' + (isDown ? 'Up' : 'Down')]((isDown ? 1000 : 1000), function(){
if(sbFirstSlide){ //if 'first time today' then clear for click-to-replay
sbTrigger.removeClass('firstSlide');
sbFirstSlide = false;
}
sbTrigger[(isDown ? 'remove' : 'add') + 'Class']('closeSlide').one('click', toggleSlideboxes);
if(!isDown) slideTimeout = setTimeout(toggleSlideboxes, 4000);
});
}
if(sbFirstSlide){
//not seen yet today so set a cookie for expire tomorrow, then toggle the slide boxes...
var oneDay = new Date();
oneDay.setUTCDate(oneDay.getUTCDate()+1);
oneDay.setUTCHours(0, 0, 0, 0); //set to literally day-by-day, rather than 24 hours
document.cookie=sbbcookiename+"=true;path=/;expires="+oneDay.toUTCString();
toggleSlideboxes();
}else{
//already seen today so show the trigger and set a click event on it...
sbTrigger.removeClass('firstSlide').one('click', toggleSlideboxes);
}
}
})();
© Stack Overflow or respective owner