Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to save data on local system using Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 542
    Comment on it

    Hello Reader's if you want to store some data on user local system so that even he turn off or refresh the webpage the data will still remains the same then you can use the Javascript for that.

    Let's consider an example where you have an counter which store number of times user hits the button. Suppose user hit the button 6 times then even he refresh the page the counter will not reset, It will start from 6. The html page will go like this:-

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <p><button onclick="clickCounter()" type="button">Click to start count hit count</button></p>
    <div id="result"></div>
    <p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
    </body>
    </html>
    

    Now you just have to make the javascript that will count the hits and store in user's system For that javascript will go like this:-

    <script>
    function clickCounter() {
        if(typeof(Storage) !== "undefined") {
            if (localStorage.clickcount) {
                localStorage.clickcount = Number(localStorage.clickcount)+1;
            } else {
                localStorage.clickcount = 1;
            }
            document.getElementById("result").innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s).";
        } else {
            document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
        }
    }
    </script>
    

    Now when a user will hit the button a popup will show the count of hits. If he close the window and again open the same page to start clicking the button then the popus will start showing hit counts after the last time it was count.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: