只读
localStorage
property allows you to access a
存储
对象为
Document
's
origin
; the stored data is saved across browser sessions.
localStorage
类似于
sessionStorage
, except that while data stored in
localStorage
has no expiration time, data stored in
sessionStorage
gets cleared when the page session ends — that is, when the page is closed. (Data in a
localStorage
object created in a "private browsing" or "incognito" session is cleared when the last "private" tab is closed.)
Data stored in either
localStorage
is specific to the protocol of the page
. In particular, data stored by a script on a site accessed with HTTP (e.g.,
http://example.com
) is put in a different
localStorage
object from the same site accessed with HTTPS (e.g.,
https://example.com
).
The keys and the values are
always
in the UTF-16
DOMString
format, which uses two bytes per character. As with objects, integer keys are automatically converted to strings.
myStorage = window.localStorage;
A
存储
object which can be used to access the current origin's local storage space.
SecurityError
文件:
or
data:
scheme, for example). For example, the user may have their browser configured to deny permission to persist data for the specified origin.
The following snippet accesses the current domain's local
存储
object and adds a data item to it using
Storage.setItem()
.
localStorage.setItem('myCat', 'Tom');
The syntax for reading the
localStorage
item is as follows:
const cat = localStorage.getItem('myCat');
The syntax for removing the
localStorage
item is as follows:
localStorage.removeItem('myCat');
The syntax for removing all the
localStorage
items is as follows:
localStorage.clear();
注意 : Please refer to the 使用 Web 存储 API article for a full example.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'localStorage' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
localStorage
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 8 | Opera 10.5 | Safari 4 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 4 | Opera Android 11 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
完整支持
Window
applicationCache
caches
closed
console
controllers
crossOriginIsolated
crypto
customElements
defaultStatus
devicePixelRatio
dialogArguments
目录
document
event
frameElement
frames
fullScreen
history
indexedDB
innerHeight
innerWidth
isSecureContext
isSecureContext
length
localStorage
location
locationbar
menubar
mozAnimationStartTime
mozInnerScreenX
mozInnerScreenY
mozPaintCount
名称
navigator
onabort
onafterprint
onanimationcancel
onanimationend
onanimationiteration
onappinstalled
onauxclick
onbeforeinstallprompt
onbeforeprint
onbeforeunload
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncuechange
ondblclick
ondevicelight
ondevicemotion
ondeviceorientation
ondeviceorientationabsolute
ondeviceproximity
ondragdrop
ondurationchange
onended
onerror
onfocus
onformdata
ongamepadconnected
ongamepaddisconnected
ongotpointercapture
onhashchange
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onlanguagechange
onload
onloadeddata
onloadedmetadata
onloadend
onloadstart
onlostpointercapture
onmessage
onmessageerror
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onmozbeforepaint
onpaint
onpause
onplay
onplaying
onpointercancel
onpointerdown
onpointerenter
onpointerleave
onpointermove
onpointerout
onpointerover
onpointerup
onpopstate
onrejectionhandled
onreset
onresize
onscroll
onselect
onselectionchange
onselectstart
onstorage
onsubmit
ontouchcancel
ontouchstart
ontransitioncancel
ontransitionend
onunhandledrejection
onunload
onuserproximity
onvrdisplayactivate
onvrdisplayblur
onvrdisplayconnect
onvrdisplaydeactivate
onvrdisplaydisconnect
onvrdisplayfocus
onvrdisplaypointerrestricted
onvrdisplaypointerunrestricted
onvrdisplaypresentchange
onwheel
opener
origin
outerHeight
outerWidth
pageXOffset
pageYOffset
parent
性能
personalbar
pkcs11
screen
screenLeft
screenTop
screenX
screenY
scrollbars
scrollMaxX
scrollMaxY
scrollX
scrollY
self
sessionStorage
sidebar
speechSynthesis
status
statusbar
toolbar
top
visualViewport
window
alert()
atob()
back()
blur()
btoa()
cancelAnimationFrame()
cancelIdleCallback()
captureEvents()
clearImmediate()
clearInterval()
clearTimeout()
close()
confirm()
convertPointFromNodeToPage()
convertPointFromPageToNode
createImageBitmap()
dump()
fetch()
find()
focus()
forward()
getAttention()
getComputedStyle()
getDefaultComputedStyle()
getSelection()
home()
matchMedia()
minimize()
moveBy()
moveTo()
open()
openDialog()
postMessage()
print()
prompt()
queueMicrotask()
releaseEvents()
requestAnimationFrame()
requestFileSystem()
requestIdleCallback()
resizeBy()
resizeTo()
restore()
routeEvent()
scroll()
scrollBy()
scrollByLines()
scrollByPages()
scrollTo()
setCursor()
setImmediate()
setInterval()
setTimeout()
showModalDialog()
sizeToContent()
stop()
updateCommands()
event
afterprint
animationcancel
animationend
animationiteration
beforeprint
beforeunload
blur
copy
cut
DOMContentLoaded
error
focus
hashchange
languagechange
load
message
messageerror
offline
online
orientationchange
pagehide
pageshow
paste
popstate
rejectionhandled
storage
transitioncancel
unhandledrejection
unload
vrdisplayconnect
vrdisplaydisconnect
vrdisplaypresentchange