In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use localStorage and sessionStorage objects". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use localStorage and sessionStorage objects".
There is no time limit for the data stored by the localStorage object. After the next day, the second week, or the next year, the data is still available.
Example
LocalStorage.sitename= "Rookie course"
Document.getElementById ("result") [xss_clean] = "website name:" + localStorage.sitename
Instance resolution:
Use key= "sitename" and value= "rookie tutorial" to create a localStorage key / value pair.
Retrieve the value with the key value "sitename" and insert the data into the element of id= "result".
The above example can also be written as follows:
/ / Storage
LocalStorage.sitename= "Rookie course"
/ / find
Document.getElementById ("result") [xss_clean] = localStorage.sitename
Remove "sitename" from localStorage:
LocalStorage.removeItem ("sitename")
The same API can be used for both localStorage and sessionStorage. The following are commonly used (take localStorage as an example):
Save data: localStorage.setItem (key,value)
Read data: localStorage.getItem (key)
Delete individual data: localStorage.removeItem (key)
Delete all data: localStorage.clear ()
Get the key:localStorage.key (index) of an index
Tip: key / value pairs are usually stored as strings, and you can convert this format as needed.
The following example shows the number of times a user clicks a button.
The string values in the code are converted to numeric types:
Example
If (localStorage.clickcount)
{
LocalStorage.clickcount=Number (localStorage.clickcount) + 1
}
Else
{
LocalStorage.clickcount=1
}
Document.getElementById ("result") [xss_clean] = "you have clicked the button" + localStorage.clickcount + "times"
SessionStorage object
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:
Example
If (sessionStorage.clickcount)
{
SessionStorage.clickcount=Number (sessionStorage.clickcount) + 1
}
Else
{
SessionStorage.clickcount=1
}
Document.getElementById ("result") [xss_clean] = "you have clicked this button in this session" + sessionStorage.clickcount + "times"
Thank you for your reading, the above is the content of "how to use localStorage and sessionStorage objects". After the study of this article, I believe you have a deeper understanding of how to use localStorage and sessionStorage objects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.