Writing Cookies Via Javascript
I am using this code to open an html pop up, to which is wish to add a 'don't show again' button that will store a preference in a cookie so that the pop up won't be shown again Wh
Solution 1:
Try this:
document.cookie = 'dontShow=1; expires=Wed, 01 Jan 2020 12:00:00 GMT; path=/';
functionreadCookie(option) {
var optionEQ = option + '=';
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, c.length);
}
if (c.indexOf(optionEQ) === 0) {
return c.substring(optionEQ.length, c.length);
}
}
returnfalse;
}
if (!readCookie('dontShow')) {
//run your popup code
}
Edit: Or, as a custom jQuery plugin:
document.cookie = 'dontShow=1; expires=Wed, 01 Jan 2020 12:00:00 GMT; path=/';
(function($) {
$.fn.readCookie = function(option) {
var optionEQ = option + '=';
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, c.length);
}
if (c.indexOf(optionEQ) === 0) {
return c.substring(optionEQ.length, c.length);
}
}
returnfalse;
}
})(jQuery);
if (!$(document).readCookie('dontShow')) {
$('#cookie-status').html('<p>Cookie not set.</p>');
} else {
$('#cookie-status').html('<p>Cookie has been set.</p>');
}
Hope this helps! :)
Post a Comment for "Writing Cookies Via Javascript"