Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
retrieving localstorage data on load javascript

I have this code that is trying to retrieve data from localstorage. However, nothing happens at all when it is run and there are no console errors.

    var score = localStorage.getItem('score') || 0;
    
    function save() {
    counter = localStorage.getItem('score', score)
    }
<body onload="save()">
        <p id="score">0</p>
        <img src="popcat1.png" alt="Invalid" id="popcat1">

I don't know I'm doing wrong and I want the score not to reset on refresh. Please tell me if there is a better way.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Local storage is not a complicated thing. There are only 4 basic operations.

Set data in localStorage:

localStorage.setItem('name', variable)

Get data from localStorage:

localStorage.getItem('name')

Remove from localStorage

localStorage.removeItem('name')

Remove all from localStorage

localStorage.clear()

In your code, you need to setItem first and than you can get the value.

about 4 years ago · Juan Pablo Isaza Report

0

Just use setItem on save rather than getItem:

let score = localStorage.getItem('score') || 0
function save() {
  localStorage.setItem('score', score)
  //           ^^^^^^^
}

For more information check out the MDN docs.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!