Reputation: 1
Is it posibble to store data to your web page permanently or after page refresh?
$('<li>').text(post).prependTo('.userPosts');
//store the data whenever the an <li> is added to the <ul class="userPosts">
i have created a jsfiddle for it http://jsfiddle.net/bgb4h2g1/1/ any ideas can be helpful. thanks!
Upvotes: 0
Views: 2067
Reputation: 707486
The javascript context lives ONLY for the lifetime of the page in the browser. As soon as the user moves to another page, the entire javascript context is thrown away. So, Javascript variables themselves do not persist beyond the lifetime of the page in the browser.
Thus, to make a value persist, you have to store it somewhere and then load it from that location the next time that page is viewed. The data can be stored either in the browser or on the server.
In the browser, you can store it in a cookie or in LocalStorage.
On the server, you can use Ajax to push a value to the server to be stored by the server however it deems to store it (in a database, in memory, etc...). If storing on the server, you will also have to know which user you are storing it on behalf of because the server serves the needs of many viewers.
If storing it in the browser, then you would usually have some javascript that runs at page initialization time that would load the data from the cookie or LocalStorage.
If storing on the server, you can either insert the data into the page as it is requested by the browser (part of the page generation) or you can have Javascript in the page request the data from the server via Ajax.
Upvotes: 1