简体   繁体   中英

jQuery toggle cookie function

I have simple toggle (button switch) function here, but I don't know how to add cookie and how to remember position when users clicks on button.

Anyone can help me to add cookie here ?

$(document).ready(function(){ 
 $("a.switch_thumb").toggle(function(){
  $(this).addClass("swap"); 
  $("ul.display").fadeOut(100, function() {
  $(this).fadeIn(100).addClass("thumb_view"); 
     });
  },

function () {
 $(this).removeClass("swap");
 $("ul.display").fadeOut(100, function() {
 $(this).fadeIn(110).removeClass("thumb_view");
    });

}); 
});

check out this pluggin https://github.com/carhartl/jquery-cookie is very simple, after you include it you could just add a flag like

$.cookie('cookie_button', 'pressed'); //when pressed or
$.cookie('cookie_button', 'not_pressed'); //if is the case

Then check the cookie value when loading the page so you can remember the users last selection

Add this to ur head

<script>
/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
</script>
<script type="text/javascript">
$(document).ready(function(){


    if($.cookie("button")=="thumb_view"){
    $('ul.display').addClass($.cookie("button"));
    $("a.switch_thumb").addClass("swap");

    }else{
    $('ul.display').removeClass($.cookie("button"));
    $("a.switch_thumb").removeClass("swap");

    }



    $("a.switch_thumb").toggle(function(){

      $(this).addClass("swap"); 
      $("ul.display").fadeOut("fast", function() {
        $(this).fadeIn("fast").addClass("thumb_view");          
         });
      $.cookie("button", "thumb_view");  

      }, function () {
      $.cookie("button", "not_thumb_view");
      $(this).removeClass("swap");
      $("ul.display").fadeOut("fast", function() {
        $(this).fadeIn("fast").removeClass("thumb_view");

        }); 
      $.cookie("button", "not_thumb_view");

    }); 




});
</script>

What about this? This will set the button_cookie to value 1 or 0 according to the fact, whether user clicked on button even or odd times

$(document).ready(function(){ 
    $("a.switch_thumb").toggle(
        function(){
            $(this).addClass("swap"); 
            $("ul.display").fadeOut(100, function() {
                $(this).fadeIn(100).addClass("thumb_view"); 
            });
            setCookie('button_pressed',!$(this).hasClass("swap"));
        },
        function () {
            $(this).removeClass("swap");
            $("ul.display").fadeOut(100, function() {
                $(this).fadeIn(110).removeClass("thumb_view");
            });
            setCookie('button_pressed',$(this).hasClass("swap"));
        }
    ); 
});

/**
 * implementation of setCookie function
 * this function creates only session cookie, can be amended so to use expires param
 */
function setCookie(name, value){
    document.cookie=name+'='+value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM