How to store variable in session storage

ncti

I making increase font size by buttons but I don't know how to store fonts sizes in session. Each time when I change page fonts size are reset. I trying use sessionStorage but probably I'm doing it wrong.

(function ($) {

    var defaultFontSize = $('html').css('font-size');

    $(".resetMe").click(function () {
        $('html').css('font-size', defaultFontSize);
    });

    $(".increase").click(function () {
        var fontSize = getFontSize();
        var newFontSize = fontSize * 1.15;
        if (newFontSize > 11.5) {
            return false;
        } else {
            setFontSize(newFontSize);
            storeFontSize(newFontSize);
            return false;
        }

        return false;
    });

    $(".decrease").click(function () {
        var fontSize = getFontSize();
        var newFontSize = fontSize * 0.85;
        if (newFontSize < 8) {
            return false;
        } else {
            setFontSize(newFontSize);
            storeFontSize(newFontSize);
            return false;
        }

        return false;
    });

    function getFontSize() {
        var currentSize = $("html").css("font-size");
        var currentSizeNumber = parseFloat(currentSize, 12);
        if (currentSizeNumber > 12) {
            return false;
        }
        if (currentSizeNumber < 10) {
            return false;
        }
        return currentSizeNumber;

    }

    function setFontSize(size) {
        $("html").css("font-size", size);
        $(".actualSize").html(size);


    }

    function storeFontSize(size) {
        if (sessionStorage) {
            sessionStorage.setItem('font-size', size);
            console.log(sessionStorage.length) // 1 element
            // 1st element
            console.log(sessionStorage.key(0)); //
            
            console.log(sessionStorage.length) // 1 - tylko
            sessionStorage.getItem('font-size');
            $("html").css("font-size",  sessionStorage.getItem('font-size'));
            $(".actualSize").html( sessionStorage.getItem('font-size'));

        }
    }

})(jQuery);

I see in console that some value is storage but I can't set this font size in session.

Reyno

You need to read the sessionStorage when the page loads. This can be done with $(document).ready

(function ($) {
  $(document).ready(function () {
    var size = sessionStorage.getItem("font-size") || 12;
    setFontSize(size);
  });

  var defaultFontSize = $("html").css("font-size");

  $(".resetMe").click(function () {
    $("html").css("font-size", defaultFontSize);
    storeFontSize(defaultFontSize);
  });

  $(".increase").click(function () {
    var fontSize = getFontSize();
    var newFontSize = fontSize * 1.15;

    if (newFontSize < 20) {
      // Not higher then 20
      storeFontSize(newFontSize);
    }
  });

  $(".decrease").click(function () {
    var fontSize = getFontSize();
    var newFontSize = fontSize * 0.85;

    if (newFontSize > 8) {
      // Not lower then 8
      storeFontSize(newFontSize);
    }
  });

  function getFontSize() {
    var currentSize = $("html").css("font-size");

    return parseFloat(currentSize, currentSize);
  }

  function setFontSize(size) {
    $("html").css("font-size", size + "px");
    $(".actualSize").html(size);
  }

  function storeFontSize(size) {
    console.log(size);
    sessionStorage.setItem("font-size", size);

    setFontSize(size);
  }
})(jQuery);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to store a variable in session?

How to store the variable throughout the session?

How to store a variable in local storage?

Session Storage: how to store multiple objects

How to store the token in Local or session storage in android?

How to save session storage item to variable

How to store a primary key in a session variable

How to store a variable inside a JavaScript session cookie?

how to store session data into custom storage in bot builder

How do I store and retrieve an inputted date using session storage?

Store Variable in Session Meteor

How to save jwt token from session storage into variable?

How to store data like jwt token in Angular 8? Is there another way to store safely using localstorage or session storage?

How to store and get image from a image variable (SESSION)?

how to store and retrieve multiple values inside single session variable

Ionic - How to store session token as globally (for app) accessible variable?

How can I store and retrieve Django session variable to/from database?

How can store javascript variable to php session variables?

Store php exec in a session variable

Store information using session variable

Store python requests session in persistent storage

How to store the user in a session

How to store data in session

How to store a value in a session?

Puppeteer: how to store a session (including cookies, page state, local storage, etc) and continue later?

How can I store in session storage so that the data is removed upon closing the browser?

set session variable or local storage in javascript

How to clear session storage in angularjs?

How works laravel session storage