If you want to use cookies, try this sample:
$(function() {
function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
$('.badge').click(function(){
var
cookieValue = +getCookie('counter'); // get cookie
$('.badge').css('display' ,'none');
document.cookie = 'counter=' + (cookieValue + 1); // setting a new value
});
});
But, as noticed @guradio, you can use localStorage to do that:
$(function() {
$('.badge').click(function(){
var
counter = +localStorage['counter']; // get counter's value from localStorage
$('.badge').css('display' ,'none');
localStorage['counter'] = (counter + 1) // setting a new value
});
});
localStorage makes your code more compact an elegant :)
The Web Storage API provides mechanisms by which browsers can store
key/value pairs, in a much more intuitive fashion than using cookies.
The two mechanisms within Web Storage are as follows:
sessionStorage maintains a separate storage area for each given origin that's available for the duration of the page session (as long
as the browser is open, including page reloads and restores)
localStorage does the same thing, but persists even when the browser is closed and reopened.
sessionStorage is similar to localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends.