1

Html code index.html this is my html code here i just put the button and a text input.i want to get the text input value there on the page even we refresh it(means that old vaue remains there on the page)

<div id="result">div</div> 
<input type="text" id="tt" />
<button id="btn" onClick="calls()">submit</button>

here is the script code ,i am using this code for the local storage but it doesnot work after the page refreshing.

function calls()
{
if(typeof(Storage)!=="undefined")
  {


  localStorage.lastname=document.getElementById("tt").value; ;
  document.getElementById("result").innerHTML="Last name: " + localStorage.lastname;
  }
else
  {
  document.getElementById("result").innerHTML="Sorry, your browser does not support web     storage...";

  }}
Joe
  • 15,205
  • 8
  • 49
  • 56

3 Answers3

1

If you use jquery then you can access local storage easily like this

localStorage["check"] = 1;

After refreshing page you will get same value

alert(localStorage["check"]);
commit
  • 4,777
  • 15
  • 43
  • 70
0

Yes, loading the file locally means that it doesn't have an origin. Since localStorage is uses the same-origin policy to determine access to stored data, it is undefined what happens when you use it with local files, and likely that it won't be persisted.

You will need to host your file on a web server in order to have a proper origin; you can just run Apache or any other server locally and access it via localhost.

Taken from: localStorage doesn't retrieve values after page refresh

Community
  • 1
  • 1
abc123
  • 17,855
  • 7
  • 52
  • 82
0

If you're already using jQuery, I'd be tempted to suggest using cahartl's jquery cookie plugin - we've been using it in work for small educational activities where students would need to switch between pages, for example, and it works really well.

GitHub link

exafred
  • 961
  • 8
  • 7