W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
該sessionStorage屬性允許您訪問(wèn)當(dāng)前源的會(huì)話Storage對(duì)象。sessionStorage類似于Window.localStorage; 唯一的區(qū)別是當(dāng)存儲(chǔ)在localStorage中的數(shù)據(jù)沒(méi)有到期設(shè)置時(shí),存儲(chǔ)在sessionStorage中的數(shù)據(jù)在頁(yè)面會(huì)話結(jié)束時(shí)被清除。只要瀏覽器處于打開(kāi)狀態(tài),頁(yè)面會(huì)話就會(huì)持續(xù),并且會(huì)在頁(yè)面重新加載和恢復(fù)后繼續(xù)存在。在新選項(xiàng)卡或窗口中打開(kāi)頁(yè)面將導(dǎo)致啟動(dòng)新會(huì)話,這與會(huì)話cookie的工作方式不同。
需要注意的是,存儲(chǔ)在sessionStorage或localStorage 中的數(shù)據(jù)特定于頁(yè)面協(xié)議的。
// Save data to sessionStorage
sessionStorage.setItem('key', 'value');
// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');
// Remove saved data from sessionStorage
sessionStorage.removeItem('key');
// Remove all saved data from sessionStorage
sessionStorage.clear();
一個(gè)Storage對(duì)象。
以下代碼段訪問(wèn)當(dāng)前域的會(huì)話Storage對(duì)象,并使用Storage.setItem()向它添加數(shù)據(jù)項(xiàng):
sessionStorage.setItem('myCat', 'Tom');
以下示例自動(dòng)保存文本字段的內(nèi)容,如果瀏覽器被意外刷新,則還原文本字段內(nèi)容,以便不會(huì)丟失寫入內(nèi)容:
// Get the text field that we're going to track
var field = document.getElementById("field");
// See if we have an autosave value
// (this will only happen if the page is accidentally refreshed)
if (sessionStorage.getItem("autosave")) {
// Restore the contents of the text field
field.value = sessionStorage.getItem("autosave");
}
// Listen for changes in the text field
field.addEventListener("change", function() {
// And save the results into the session storage object
sessionStorage.setItem("autosave", field.value);
});
規(guī)范 | 狀態(tài) | 注釋 |
---|---|---|
HTML Living Standard
該規(guī)范中“sessionStorage”的定義。
|
Living Standard
|
電腦端 | 移動(dòng)端 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome
|
Edge
|
Firefox
|
Internet Explorer
|
Opera
|
Safari
|
Android webview | Chrome for Android
|
Edge Mobile | Firefox for Android
|
Opera for Android
|
iOS Safari | |
基本支持 | 支持:5 | 支持 | 支持:2 | 支持:8 | 支持:10.5 | 支持:4 | 支持 | 支持 | 支持 | 支持 | 支持:11 | 支持:3.2 |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: