Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Storage - HTML5 Demo with Code [closed]

Tags:

I am trying to work with local storage with forms using html5. I am just unable to find a single working demo online. Can anyone find me a good demo and a tutorial that works. My browser is completely supported.

Appreciate all the help. Thanks

like image 969
Sanket Avatar asked Dec 16 '10 14:12

Sanket


People also ask

Does localStorage clear on browser close?

localStorage is similar to sessionStorage , except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed.

Does HTML5 have local storage?

Because HTML5 local storage is natively integrated into Web browsers, it is available without third-party browser plug-ins. It is described in the HTML5 specifications. Local storage is mainly used to store and retrieve data in HTML pages from the same domain.

Can I store HTML in localStorage?

Save the HTML to localStorage # The innerHTML property returns the HTML inside an element as a string, which makes it the perfect way for us to get and store our list. Let's automatically save a users list every time they add an item to it. You use the localStorage. setItem() method to save data to localStorage .


1 Answers

Here's a jsfiddle demo

(copy of the associated js code, uses of localStorage are called out in the comments)

//Note that if you are writing a userscript, it is a good idea // to prefix your keys, to reduce the risk of collisions with other  // userscripts or the page itself. var prefix = "localStorageDemo-";  $("#save").click(function () {      var key = $("#key").attr('value');     var value = $("#value").attr('value');     localStorage.setItem(prefix + key, value);      //******* setItem()     //localStorage[prefix+key] = value; also works     RewriteFromStorage(); });  function RewriteFromStorage() {     $("#data").empty();     for(var i = 0; i < localStorage.length; i++)    //******* length     {         var key = localStorage.key(i);              //******* key()         if(key.indexOf(prefix) == 0) {             var value = localStorage.getItem(key);  //******* getItem()             //var value = localStorage[key]; also works             var shortkey = key.replace(prefix, "");             $("#data").append(                 $("<div class='kvp'>").html(shortkey + "=" + value)                    .append($("<input type='button' value='Delete'>")                            .attr('key', key)                            .click(function() {      //****** removeItem()                                 localStorage.removeItem($(this).attr('key'));                                 RewriteFromStorage();                             })                           )             );         }     } }  RewriteFromStorage(); 
like image 138
Benjol Avatar answered Sep 28 '22 05:09

Benjol