Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade For Teachers Spaces Get Certified Upgrade For Teachers Spaces
   ❮     
     ❯   

HTML Tutorial

HTML HOMEHTML IntroductionHTML EditorsHTML BasicHTML ElementsHTML AttributesHTML HeadingsHTML ParagraphsHTML StylesHTML FormattingHTML QuotationsHTML CommentsHTML ColorsHTML CSSHTML LinksHTML ImagesHTML FaviconHTML Page TitleHTML TablesHTML ListsHTML Block & InlineHTML DivHTML ClassesHTML IdHTML ButtonsHTML IframesHTML JavaScriptHTML File PathsHTML HeadHTML LayoutHTML ResponsiveHTML ComputercodeHTML SemanticsHTML Style GuideHTML EntitiesHTML SymbolsHTML EmojisHTML CharsetsHTML URL EncodeHTML vs. XHTML

HTML Forms

HTML FormsHTML Form AttributesHTML Form ElementsHTML Input TypesHTML Input AttributesInput Form Attributes

HTML Graphics

HTML CanvasHTML SVG

HTML Media

HTML MediaHTML VideoHTML AudioHTML Plug-insHTML YouTube

HTML APIs

HTML Web APIsHTML GeolocationHTML Drag and DropHTML Web StorageHTML Web WorkersHTML SSE

HTML Examples

HTML ExamplesHTML EditorHTML QuizHTML ExercisesHTML WebsiteHTML SyllabusHTML Study PlanHTML Interview PrepHTML BootcampHTML CertificateHTML SummaryHTML Accessibility

HTML References

HTML Tag ListHTML AttributesHTML Global AttributesHTML Browser SupportHTML EventsHTML ColorsHTML CanvasHTML Audio/VideoHTML DoctypesHTML Character SetsHTML URL EncodeHTML Lang CodesHTTP MessagesHTTP MethodsPX to EM ConverterKeyboard Shortcuts

HTMLWeb Storage API


HTML Web Storage API; better than cookies.


What is HTML Web Storage?

With web storage, applications can store data locally within the user's browser.

Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

Web storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.


Web Storage API Objects

Web storage provides two objects for storing data in the browser:

  • window.localStorage - stores data with no expiration date (data is not lost when the browser tab is closed)
  • window.sessionStorage - stores data for one session (data is lost when the browser tab is closed)

Browser Support

The numbers in the table specify the first browser version that fully supports Web Storage.

API
localStorage4.08.03.54.011.5
sessionStorage4.08.03.54.011.5

Test Web Storage API Support

Before using web storage, we can quickly check browser support for localStorage and sessionStorage:

Example

Test browser support:

<script>
const x = document.getElementById("result");
if (typeof(Storage) !== "undefined") {
  x.innerHTML = "Your browser supports Web storage!";
} else {
  x.innerHTML = "Sorry, no Web storage support!";
}
</script>
Try it Yourself »


The localStorage Object

ThelocalStorage object stores the data with no expiration date. The data will not be lost when the browser is closed, and will be available the next day, week, or year.

Example

UselocalStorage to set and retrieve name and value pairs:

<script>
const x = document.getElementById("result");

if (typeof(Storage) !== "undefined") {
  // Store
  localStorage.setItem("lastname", "Smith");
  localStorage.setItem("bgcolor", "yellow");
  // Retrieve
  x.innerHTML = localStorage.getItem("lastname");
  x.style.backgroundColor = localStorage.getItem("bgcolor");
} else {
  x.innerHTML = "Sorry, no Web storage support!";
}
</script>
Try it Yourself »

Example explained:

  • Use thelocalStorage.setItem() method to create name/value pairs
  • Use thelocalStorage.getItem() method to retrieve the values set
  • Retrieve the value of "lastname" and insert it into an element with id="result"
  • Retrieve the value of "bgcolor" and insert it into the style backgroundColor of the element with id="result"

The syntax for removing the "lastname" localStorage item is as follows:

localStorage.removeItem("lastname");

Note: Name/value pairs are always stored as strings. Remember to convert them to another format when needed!


Counting Clicks with localStorage

The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number to be able to increase the counter:

Example

<script>
function clickCounter() {
  const x = document.getElementById("result");
  if (typeof(Storage) !== "undefined") {
    if (localStorage.clickcount) {
      localStorage.clickcount = Number(localStorage.clickcount)+1;
    } else {
      localStorage.clickcount = 1;
    }
    x.innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s)!";
  } else {
    x.innerHTML = "Sorry, no Web storage support!";
  }
}
</script>
Try it Yourself »

The sessionStorage Object

ThesessionStorage object is equal to thelocalStorage object,except that it stores the data for only one session! The data is deleted when the user closes the specific browser tab.

Counting Clicks with sessionStorage

The following example counts the number of times a user has clicked a button, in the current session:

Example

<script>
function clickCounter() {
  const x = document.getElementById("result");
  if (typeof(Storage) !== "undefined") {
    if (sessionStorage.clickcount) {
      sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
    } else {
      sessionStorage.clickcount = 1;
    }
    x.innerHTML = "You have clicked the button " + sessionStorage.clickcount + " time(s) in this session!";
  } else {
    x.innerHTML = "Sorry, no Web storage support!";
  }
}
</script>
Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp