Window.sessionStorage

Posted 人最大的荣耀不在于从未失败,而在于每次失败以后都能东山再起

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Window.sessionStorage相关的知识,希望对你有一定的参考价值。

?

The?sessionStorage?property allows you to access a session?Storage?object for the current origin. sessionStorage is similar to?Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores.?Opening a page in a new tab or window will cause a new session to be initiated,?which differs from how session cookies work.

It should be noted that data stored in either sessionStorage or localStorage?is specific to the protocol of the page.

Syntax

// 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();
								

Value

A?Storage?object.

Example

The following snippet accesses the current domain‘s session?Storage?object and adds a data item to it using?Storage.setItem().

sessionStorage.setItem(‘myCat‘,
											‘Tom‘);
													

The following example autosaves the contents of a text field, and if the browser is accidentally refreshed, restores the text field content so that no writing is lost.

// 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);
														

});
					

Note: Please refer to the?Using the Web Storage API?article for a full example.

?

From: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

以上是关于Window.sessionStorage的主要内容,如果未能解决你的问题,请参考以下文章

如何在 chrome 控制台中查看 $window.sessionStorage 的属性

window.sessionStorage、window.localStorage与cookie总结

Window.sessionStorage 会话存储

localStorage,sessionStorage的使用

本地存储(Application)

本地存储(Application)