Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Jquery carousel/toggle, Need to save the state. WordPress

  • SOLVED

Hi!
I have this problem [[LINK href="http://depositcasino.info"]]http://depositcasino.info[[/LINK]]
I want to make the yellow bonus bar save the state as I think it would be really annoying to keep having to minimize the bar everytime a page loads - How can i get it to save the toggle state?

<script>
var visible = false;

$(document).ready(function(){

$("#bonuses-container #bonuses").slideDown("slow", function(){
$("#bonuses-container #bonuses .carousel").jcarousel({
auto:5,
scroll:1,
animation:500,
wrap:"circular",
initCallback:initCarousel
});
visible = true;
});

function initCarousel(carousel){
$("#bonuses-container .bonuses-btn a").click(function(){
if(visible)
carousel.startAuto();
else
carousel.stopAuto();
visible = !(visible);
$("#bonuses-container #bonuses").slideToggle("slow");
return false;
});
}
</script>


<?php if (is_user_logged_in() ) { //only logged in user can see this
}
else { ?>
<div id="bonuses-container">
<div class="bonuses-btn"><a href="#">Bonuses</a></div>
<div id="bonuses">
<?php wp_reset_query();
if ( is_page('bingo') ) {
include(TEMPLATEPATH . '/bingo-bar.php');
} elseif ( is_page('poker') ) {
include(TEMPLATEPATH . '/poker-bar.php');
} elseif ( is_page('sports') ) {
include(TEMPLATEPATH . '/sports-bar.php');
} elseif ( is_page('scratchies') ) {
include(TEMPLATEPATH . '/scratchies-bar.php');
} else {
include(TEMPLATEPATH . '/casinos-bar.php');
} ?>
</div>
</div>
<?php } ?>

Answers (2)

2010-10-06

John Cotton answers:

Clearly you could store the state with your user data so that it persisted for ever, but won't it be sufficient (and much easier) to save it as a cookie?

If so, add [[LINK href="http://plugins.jquery.com/project/Cookie"]]jquery Cookie[[/LINK]] to your site and then something along the following lines in your code:

// Save
$.cookie('bonusBarState', 'Visible', { expires: new Date(2036, 11, 31) });


//Retrieve
var l_bonusBarState = $.cookie('bonusBarState');

Obviously those lines need to go in the existing JS. If you need me to show you where, just ask.

Regards

John


I'm not quite sure I follow your JS code: you seem to slide it down first regardless - is that right?

My guess is that you want something like this:



<script>
var bonusBarState = $.cookie('bonusBarState') == null ? false : $.cookie('bonusBarState');

function initCarousel(carousel){

$("#bonuses-container .bonuses-btn a").click(function(){

if(bonusBarState) {
carousel.startAuto();
} else {
carousel.stopAuto();
}


bonusBarState = !(bonusBarState );

$.cookie('bonusBarState', bonusBarState, { expires: new Date(2036, 11, 31) });

$("#bonuses-container #bonuses").slideToggle("slow");

return false;
});
}


$(document).ready(function(){

// Could change this if some default CSS is set instead

if(bonusBarState) {
$("#bonuses-container #bonuses").show();
} else {
$("#bonuses-container #bonuses").hide();
}

$("#bonuses-container #bonuses .carousel").jcarousel({

auto:5,
scroll:1,
animation:500,
wrap:"circular",
initCallback:initCarousel
});
});
</script>



You may want to decide what you start state is and then use CSS to set that, but see comment in the code.


CHANGE THIS LINE TO

var bonusBarState = $.cookie('bonusBarState') == null ? true : $.cookie('bonusBarState');

<strong>Have you got a server I can see?</strong>

<em>Also, have you included the jquery cookie.js file?</em>



pandragon comments:

Thanks John, Where would I place that bit of code in the existing code?


pandragon comments:

I tried that didn't work :u just made it not open at all. any other way around it?


pandragon comments:

<script>
var bonusBarState = $.cookie('bonusBarState') == null ? false : $.cookie('bonusBarState');
function initCarousel(carousel){
$("#bonuses-container .bonuses-btn a").click(function(){
if(bonusBarState) {
carousel.startAuto();
} else {
carousel.stopAuto();
}
bonusBarState = !(bonusBarState );
$.cookie('bonusBarState', bonusBarState, { expires: new Date(2036, 11, 31) });
$("#bonuses-container #bonuses").slideToggle("slow");
return false;
});
}
$(document).ready(function(){
// Could change this if some default CSS is set instead
if(bonusBarState) {
$("#bonuses-container #bonuses").show();
} else {
$("#bonuses-container #bonuses").hide();
}
$("#bonuses-container #bonuses .carousel").jcarousel({
auto:5,
scroll:1,
animation:500,
wrap:"circular",
initCallback:initCarousel
});
});
</script>


Have this in there but seems not to save state when you click on another page still reappears


pandragon comments:

sent you server info :) the stuff is in the header.php and footer.php files in pan theme

2010-10-06

Panagiotis Grigoropoulos answers:

Oh second ! Anyways ! Hope you fix your issue


pandragon comments:

hehe Thanks for both your quick replies :)


pandragon comments:

Hey Panagiotis did you wanna try? Still doesnt work, I think cookie.js is either too old or hacked script as my server thinks its an attack.

So i need a solution that doesn't involve that script and still cookies users preference.