简体   繁体   中英

jQuery: How can I save a selection in a cookie?

I created a changer of banner that allows the user to change the banner as he want, but I would like that the selection is saved in a cookie. I would like to create my own code. You can have an overview of what the code does here: http://thoughtscenter.com/test/

Currently, if I select an image when banner picker is open, everything goes well, but when I refresh the page, the image disappears and I must select it again to put the image: http://prntscr.com/7mkzi2 after I refresh the page: http://prntscr.com/7mkzn4

My HTML code (changer of banner):

           <div id="banner-picker">
                <ul>
                    <li>
                        <span id="banner-1" style="background-image: url('banner-1.jpg');"></span>
                    </li>
                    <li>
                        <span id="banner-2" style="background-image: url('banner-2.jpg');"></span>
                    </li>
                    <li>
                        <span id="banner-3" style="background-image: url('banner-3.jpg');"></span>
                    </li>
                    <li>
                        <span id="banner-4" style="background-image: url('banner-4.jpg');"></span>
                    </li>
                </ul>
            </div>

JavaScript code of the changer of banner:

$(function()
{
    $("#banner-picker-toggle-button").click(function(e)
    {
        $("#banner-picker").slideToggle();
    });
    $("#banner-picker span").click(function(e)
    {
       var banner_id = $(this).attr("id");
       $("#header").removeClass().addClass(banner_id);
    });
});

CSS:

    #banner-picker
    {
        border-bottom: 1px solid #CCCCCC;
        display: none;
        overflow: hidden;
        margin: 0 -20px 20px -20px;
        padding: 0 20px 20px 20px;
    }

    #banner-picker li
    {
        float: left;
        width: 25%;
    }

    #banner-picker span
    {
        height: 130px;
        display: block;
        margin-right: 20px;
        border-radius: 3px;
        box-shadow: inset 0px 1px 10px;
    }

    #banner-picker li:last-child span
    {
        margin-right: 0;
    }

#header.banner-1
{
    background-image: url("banner-1.jpg");
}

#header.banner-2
{
    background-image: url("banner-2.jpg");
}

#header.banner-3
{
    background-image: url("banner-3.jpg");
}

#header.banner-4
{
    background-image: url("banner-4.jpg");
}

The idea is to save what the user selected via a cookie for that at the next visit or when he refreshes the page, the banner remains the same.

just see in this code you can use like this.

$(function()
{
          var bannervalue= localStorage.getItem('banner'); // get banner id from storage
          if(bannervalue==null)
          {
          }
          else
          {
            $("#header").removeClass().addClass(bannervalue);
          }



    $("#banner-picker-toggle-button").click(function(e)
    {
        $("#banner-picker").slideToggle();
    });
    $("#banner-picker span").click(function(e)
    {
       var banner_id = $(this).attr("id");
       $("#header").removeClass().addClass(banner_id);
       localStorage.setItem('banner', banner_id ); // set banner id from storage
    });
});

You can try using jQuery cookies plugin as localStorage will not work in ie9 or lower:-

add this js file <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.3.1/jquery.cookie.js"></script>

and change your code:-

 $("#banner-picker span").click(function(e)
 {
   var banner_id = $(this).attr("id");
   $.cookie('banner',banner_id);
   $("#header").removeClass().addClass(banner_id);
});

and also add this code:-

$(function(){
 if(typeof $.cookie('banner')!='undefined')
 {
  $("#header").addClass($.cookie('banner'));
 }
});

UPdated Code:=

$(function () {
  if (typeof $.cookie('banner') != 'undefined') {
     $("#header").addClass($.cookie('banner'));
  }
  $("#banner-picker-toggle-button").click(function (e) {
    $("#banner-picker").slideToggle();
  });
  $("#banner-picker span").click(function (e) {
    var banner_id = $(this).attr("id");
    $.cookie('banner', banner_id);
    $("#header").removeClass().addClass(banner_id);
  });
 });

Use document.cookie="key:value" to set a cookie where the key (variable name) is key and the value of the variable is value.

You can use this to get a cookie:

function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}

where cname is the name of the cookie.

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