Cookies add to hit button

I have a problem adding a cookie. Read a couple of answers, but it's hard to understand if you've never worked with them.

Basically I want to add a cookie if someone clicked on the specified button. For example, if a person clicks the button "like a button", hidden content will be detected regardless of whether he will go forward / backward or refresh the page, and the cookie should be deleted after a couple of days.

What I used to hide the content is the following:

HTML:

<div id="fd">
    <p>Button from below will become active once you hit like button!</p>
    <div id="get-it">
        <a class="button"><img src="img/get-button.png"></a>
    </div>
</div>
<div id='feedback' style='display:none'></div>

JavaScript:

FB.Event.subscribe('edge.create', function (response) {
    $('#feedback').fadeIn().html('<p>Thank you. You may proceed now!</p><br/><div id="get-it"><a class="button2" href="pick.html"><img src="img/get-button.png"></a></div>');
    $('#fd').fadeOut();
});

However, if I click update or return to the content page, it will be hidden again. This is the reason I want to add a cookie when a button is clicked. Anyone who can give me some explanation or sample code? Thank.

+3
2

jQuery-cookie. :

// Create a cookie
$.cookie('the_cookie', 'the_value');

// Create expiring cookie, 7 days from then:
$.cookie('the_cookie', 'the_value', { expires: 7 });

// Read a cookie
$.cookie('the_cookie'); // => 'the_value'
$.cookie('not_existing'); // => null

// EDIT
// Attaching to a button click (jQuery 1.7+) and set cookie
$("#idOfYourButton").on("click", function () {
    $.cookie('the_cookie', 'the_value', { expires: 7 });
});

// Attaching to a button click (jQuery < 1.7) and set cookie
$("#idOfYourButton").click(function () {
    $.cookie('the_cookie', 'the_value', { expires: 7 });
});
+13

cookie localStorage.

+3

All Articles