nielsv
nielsv

Reputation: 6820

Add data to localstorage and use it on another page

Is it possible that I can save data to the local storage on a site and then use it later on another page on the same site?

For example: I have a button "add to localstorage" in this page: http://www.domain.com/exhibitors/view/41873 and when I click it I add some data to the local storage and after on I want to use it on http://wwww.domain.com/favorites/view/

Is this possible?

Upvotes: 0

Views: 5263

Answers (1)

Filippo oretti
Filippo oretti

Reputation: 49843

Yes it is possible, you just have to set it to localStorage :

localStorage.username = "User";

now you just have to retrieve it , wherever you want, like:

alert(localStorage.username);

in your case you just have to to somenthing like this:

<a href="#" onclick="localStorage.username = 'Paul'">Add to localStorage</a>

then in your view somewhere else just do:

<script> alert(localStorage.username); </script>

Usually there are 2 possibilities:

localStorage = think at it as a cookie (it persists also on browser closing)

sessionStorage = think at it as a session (it persists until you close the browser)

OBVIOUSLY

it's highly suggested to not store Sensible data in browser, so use them for routines but not for storing passwords for example, cause, user can edit them from his browser, and malicious users can too

Upvotes: 3

Related Questions