Saturday, 8 June 2013

One cookie for multiple wordpress pages

One cookie for multiple wordpress pages

I am using the following script to set and retrieve a cookie for view modes (i.e., grid-view, full-view) on a wordpress blog. It works great, except if you go to the second page of posts, it doesn't maintain the same view state. Basically, each page has its own cookie. What I want is to set the cookie to be effecting ALL pages of blog posts for the loop.
Here it is using jQuery cookie:
$(document).ready(function(){
      var wrap = $('#viewMode'),
          viewMode = $.cookie( 'view-mode' );
      wrap.children().hide();

      $('.js-view-mode').on( 'click', 'a',function( e ){
        e.preventDefault();
        var t = $(this),
            type = t.attr('href');

        if( t.parent().hasClass('s') ) return; 

        t.parent().addClass('s')
        .siblings().removeClass('s');

         var lheight = $("#viewMode").height();
      if(lheight != 0){
   $("#viewMode").css("height",lheight+"px");        
      }
      wrap.children().fadeOut(); $(type).delay(500).fadeIn(function(){
          $("#viewMode").css("height","auto");
      });

        viewMode = $.cookie( 'view-mode', type );

      });

      if ( viewMode ) {
        $('.js-view-mode a[href='+ viewMode +']').trigger('click');
      } else {
        $('.js-view-mode li:first a').trigger( 'click' );
      }

    });

No comments:

Post a Comment