how to make the localStorage so that the currents <p> Did the JavaScript disappear after reloading?</p>



  • js

    function fuf() {
        let ghj = document.querySelector('body');
        ghj.innerHTML += "<p>приветик</p>";
    }
    

    html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width">
        <title>Document</title>
    </head>
    <body onload="fyf()">
        <button onclick="fuf()">кликЕР</button>
        <script src="code.js"></script>
    </body>
    </html>
    


  • In localStorage, you can only keep the line and have the methods setItem and getItem. the method setItem https://developer.mozilla.org/ru/docs/Web/API/Window/localStorage

    localStorage.set('key', "<p>приветик</p>")
    

    Use it. localStorage.get('key')

    And that's the code.

    function fuf() {
        let ghj = document.querySelector('body');
        const element = "<p>приветик</p>";
        localStorage.setItem('key', element)
        ghj.innerHTML += localStorage.getItem('key');
    }
    

    This code is far from perfect, but I think it'll be clear how localStorage works.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2