I'm kind of new to JavaScript. I've created a function that changes the font size and I would like to save the size in a cookie, so that the user won't have to change it every time. But I haven't got the cookie part to work.
I'm using the following libraries: jQuery JavaScript Library v1.11.1 and https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
function change_size(element, size) {
var new_size;
var current = parseInt(docCookies.getItem("FontSize"));
if (current !== "") {
current = parseInt(element.css('font-size'));
}
if (size === 'smaller') {
if (current > 12) {
new_size = current - 2;
}
}
else if (size === 'bigger') {
if (current < 22) {
new_size = current + 2;
}
}
element.css('font-size', new_size + 'px');
docCookies.setItem("FontSize", num.toString(new_size), Infinity);
}
$('#smaller').click(function() {
change_size($('p'), 'smaller');
});
$('#bigger').click(function() {
change_size($('p'), 'bigger');
});
Change this line
docCookies.setItem("FontSize", num.toString(new_size), Infinity);
to
docCookies.setItem("FontSize", new_size, Infinity);
You don't have any num
variable.
Also don't forget to set new font size on page load:
var fontSize = docCookies.getItem("FontSize");
if (fontSize) {
$('p').css('font-size', fontSize + 'px');
}
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.