How can we add and remove data from local storage?(ASP.NET HTML 5 interview questions)
- By Shiv Prasad Koirala in ASP.NET
- Dec 11th, 2013
- 15601
- 0
Data is added to local storage using "key" and "value". Below sample code shows country data "India" added with key value "Key001".
localStorage.setItem("Key001","India");
To retrieve data from local storage we need to use "getItem" providing the key name.
var country = localStorage.getItem("Key001");
You can also store JavaScript object's in the local storage using the below code.
var country = };
country.name = "India";
country.code = "I001";
localStorage.setItem("I001", country);
var country1 = localStorage.getItem("I001");
If you want to store in JSON format you can use "JSON.stringify" function as shown in the below code.
localStorage.setItem("I001",JSON.stringify(country));
What is the lifetime of local storage?
Local storage does not have a life time it will stay until either the user clear it from the browser or you remove it using JavaScript code.
Below is a ASP.NET interview question with answer video which explains how response.redirect is different from server.transfer.
Shiv Prasad Koirala
Visit us @ www.questpond.com or call us at 022-66752917... read more