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

87
Views
localStorage Remember Last Tab State

There are loads of exmaple of saving tab state with jQuery but not with Vanilla JS - I want to remember last tab state in the event a user leaves and returns to page, refreshes page etc. Would it be better to a sessions variable or is localstorage the best option ?

 <html>
 <head>
 <meta charset="utf-8">
<style>
    .red {
        background:red;
        color:#fff;
    }
    
    </style>
</head>

<body>
                <div class="container">
                    <div class="bar blue">
                            <button class="bar-item button tablink red" onclick="openTab(event,'Summary')">Summary</button>
                            <button class="bar-item button tablink" onclick="openTab(event,'Finance')">Finance</button>
                            <button class="bar-item button tablink" onclick="openTab(event,'Enquire')">Enquire</button>
                        <button class="bar-item button tablink" onclick="openTab(event,'Save')">Save</button>
                    </div>
                    <div id="Summary" class="container border vehTab">
                       FIRST TAB
                    </div>
                    <div id="Finance" class="container border vehTab" style="display:none">
                      SECOND TAB
                    </div>
                    <div id="Enquire" class="container border vehTab" style="display:none">
                      THIRD TAB                     
                        </div>
                        <div id="Save" class="container border vehTab" style="display:none">                                            
                      FOURTH TAB
                    </div>
        </div>
 <script>
 function openTab(evt, tabName) {
     var i, x, tablinks;
     x = document.getElementsByClassName("vehTab");
     for (i = 0; i < x.length; i++) {
         x[i].style.display = "none";
     }
     tablinks = document.getElementsByClassName("tablink");
     for (i = 0; i < x.length; i++) {
         tablinks[i].className = tablinks[i].className.replace("red", "");
     }
     document.getElementById(tabName).style.display = "block";
     evt.currentTarget.className += " red";
  }
    </script> 
   </body>
 </html>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Sessions are destroyed once the user closes the tab or browser. I would suggest localstorage over session storage in your case

about 4 years ago · Juan Pablo Isaza Report

0

Storing in session will be destroyed when the user closes the tab or browser.

Storing in local storage will be present for up to a lifetime until you clear it. If you are using local storage in secured pages (logged in user only has access to it) I suggest that it should be cleared when the user loges out

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!