WpFinder
WpFinder

Reputation: 63

How to implement localstorage for current open tab?

I am followed this link for store the data on my browser for radio buttons. Issue is If I opened same url in another tab, the radio buttons are selected as per previous tab.

Functionality is - Submit button redirect into another page. So, What I need is local storage of that particular current opened tab and particular link only.

code I am using

<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />
<title>Save state of checkbox on refresh using JavaScript</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link href="css/custom.css" rel="stylesheet" />
<style type="text/css">
</style>
</head>
<body>

<ul class="list-group">
      <li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox" value="asdf"> Must I save my state?</li>
      <li class="list-group-item"><input type="checkbox" class="save-cb-state" name="anothercheckbox" value="1"> Try and save this</li>
      <li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox2" value="qwer"> This can be saved as well.</li>
    </ul>  
<script>
// Avoid scoping issues by encapsulating code inside anonymous function
(function() {
 // variable to store our current state
  var cbstate;

  // bind to the onload event
  window.addEventListener('load', function() {

    // Get the current state from localstorage
    // State is stored as a JSON string
    cbstate = JSON.parse(localStorage['CBState'] || '{}');

    // Loop through state array and restore checked 
    // state for matching elements
    for(var i in cbstate) {
      var el = document.querySelector('input[name="' + i + '"]');
      if (el) el.checked = true;
    }

    // Get all checkboxes that you want to monitor state for
    var cb = document.getElementsByClassName('save-cb-state');

    // Loop through results and ...
    for(var i = 0; i < cb.length; i++) {

      //bind click event handler
      cb[i].addEventListener('click', function(evt) {
        // If checkboxe is checked then save to state
        if (this.checked) {
          cbstate[this.name] = true;
        }

    // Else remove from state
        else if (cbstate[this.name]) {
          delete cbstate[this.name];
        }

    // Persist state
        localStorage.CBState = JSON.stringify(cbstate);
      });
    }
  });
})();
</script>
</body>
</html> 

Upvotes: 3

Views: 2167

Answers (2)

WpFinder
WpFinder

Reputation: 63

Just change the content localstorage into sessionStorage. Below are the updated code and solution for this Question (How to implement localstorage for cureent open tab?). Answer is:

<script>
// Avoid scoping issues by encapsulating code inside anonymous function
(function() {
 // variable to store our current state
  var cbstate;

  // bind to the onload event
  window.addEventListener('load', function() {

    // Get the current state from localstorage
    // State is stored as a JSON string
    cbstate = JSON.parse(sessionStorage['CBState'] || '{}');

    // Loop through state array and restore checked 
    // state for matching elements
    for(var i in cbstate) {
      var el = document.querySelector('input[name="' + i + '"]');
      if (el) el.checked = true;
    }

    // Get all checkboxes that you want to monitor state for
    var cb = document.getElementsByClassName('save-cb-state');

    // Loop through results and ...
    for(var i = 0; i < cb.length; i++) {

      //bind click event handler
      cb[i].addEventListener('click', function(evt) {
        // If checkboxe is checked then save to state
        if (this.checked) {
          cbstate[this.name] = true;
        }

    // Else remove from state
        else if (cbstate[this.name]) {
          delete cbstate[this.name];
        }

    // Persist state
        sessionStorage.CBState = JSON.stringify(cbstate);
      });
    }
  });
})();
</script>

and Special thanks to @Jacob Nelson

Upvotes: 1

Saahil Hamayun
Saahil Hamayun

Reputation: 99

You should be able to manage it by using sessionStorage rather than localStorage. More info on the difference here: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

Upvotes: 1

Related Questions