HTML 5 Web Storage
HTML5 provides two new ways to store data on the client:
- Localstorage-Data storage with no time limit
<!DOCTYPE HTML><HTML><Body><DivID= "Result"></Div><Script> //Check Browser supportif (typeof(Storage)!== "undefined") { //StoreLocalstorage.setitem ("LastName", "Gates"); //Retrievedocument.getElementById ("result"). InnerHTML=Localstorage.getitem ("LastName");} Else{document.getElementById ("result"). InnerHTML= "I'm sorry. Your browser does not support Web Storage ...";}</Script></Body></HTML>
Storage.setitem ()setItem()
As an Storage
interface method, a key name and value are accepted as parameters, and the key name is added to the store, and the corresponding value is updated if the key name already exists.
Grammarstorage. SetItem (keyNamekeyValue);
Parameters
-
KeyName
-
A
DOMString
key name to create or update.
-
KeyValue
-
a
DOMString
value that corresponds to the key name to be created or updated.
return value None
- Sessionstorage-sessionstorage method
The Sessionstorage method stores data for a session. When the user closes the browser window, the data is deleted.
How to create and access a sessionstorage:
<Body> <DivID= "Result"></Div> <Script> if (typeof(Storage)!=="undefined") {Sessionstorage.setitem ("LastName","Gates"); document.getElementById ("result"). InnerHTML=Sessionstorage.getitem ("LastName"); }Else{document.getElementById ("result"). InnerHTML= "I'm sorry, the content you're looking for is not supported on a temporary"; } if(sessionstorage.pagecount) {Sessionstorage.pagecount=Number (Sessionstorage.pagecount)+1; }Else{Sessionstorage.pagecount= 1; } document.write ("visits the number of times"+Sessionstorage.pagecount+"Times") </Script> </Body>
Cookies and WebStorage