Add Cookie to Maintain font-size Across jQuery Mobile Pages

南笙酒味 提交于 2019-12-23 06:05:52

问题


I'm trying to add a cookie request to this code to maintain the user's selected font-size across jQM pages once they change it. Never done this before. Can anybody get me started?

<script>
$(document).ready(function () {
    $(".increaseFont,.decreaseFont").click(function () {
        var type = $(this).val();
        var curFontSize = $('.gridContainer').css('font-size');
        if (type == 'increase') {
            $('.gridContainer').css('font-size', parseInt(curFontSize) + 2);
        } else {
            $('.gridContainer').css('font-size', parseInt(curFontSize) - 2);
        }

    });


});
</script>

Thanks as always!


回答1:


Instead of a cookie, why not use localStorage? It never expires and is really easy to use within JavaScript. Use setItem to store the font size:

localStorage.setItem('FontSize', curFontSize);

Then to retrieve the font size on another page:

var curFontSize= localStorage["FontSize"];

If later you want to remove the saved font size:

localStorage.removeItem("FontSize");

Here is some documentation on localStorage: http://www.w3schools.com/html/html5_webstorage.asp




回答2:


I woudl recommend jQuery.Cookie to work with Javascript and Cookies, otherwise it can be a mess... https://github.com/carhartl/jquery-cookie



来源:https://stackoverflow.com/questions/20204206/add-cookie-to-maintain-font-size-across-jquery-mobile-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!