Cookies and WebStorage

Source: Internet
Author: User
Tags sessionstorage

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.

    Grammar
    storage. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.