In the past, we used document.cookie to store data locally, but because its storage size is only about 4K, The parsing is also very complicated, which brings a lot of inconvenience to development. But now html5 has web storage, which makes up for the shortcomings of cookies, and it is also quite convenient to open it
Web storage is divided into two categories
sessionStorage
The capacity is approx. It is about 5M, and the life cycle of this method is until the browser window is closed
##localStorage
The capacity is about 20M. The stored data will not expire when the user's browsing session expires, but will be deleted at the user's request. Browsers also delete them due to storage space limitations or security reasons. And the data stored in the type can be shared by multiple windows of the same browser
Notes: Only string can be stored. If it is jsonobject, the object can be JSON.stringify () Detailed explanation of the
method after encoding:
setItem(key, value) Set storage content
getItem(key)Read storage content
removeItem(key)Remove the key value to The storage content of key
clear() Clear all storage contents
Let’s do this Let me show you how he writes:
//更新 function update() { window.sessionStorage.setItem(key, value); } //获取 function get() { window.sessionStorage.getItem(key); } //删除 function remove() { window.sessionStorage.removeItem(key); } //清空所有数据 function clear() { window.sessionStorage.clear(); }
recording username and password
When thecheckbox for remember password is checked, the username and password do not need to be re-entered the next time you open it
html part: