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

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的更多相关文章

  1. BOM 浏览器对象模型_Storage 接口 - window.sessionStorage - window.localStorage

    Storage 接口 用于脚本在浏览器保存数据. 保存的数据都以“键值对”的形式存在.也就是说,每一项数据都有一个键名和对应的值. 所有的数据都是以文本格式保存 受同域限制 ---- 某个网页存入的数 ...

  2. Window.sessionStorage - Web API 接口参考 | MDN

    参考:https://developer.mozilla.org/zh-CN/docs/Web/API/Window/sessionStorage sessionStorage 属性允许你访问一个 s ...

  3. HTML5 sessionStorage会话存储

    sessionStorage 是HTML5新增的一个会话存储对象,用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据.本篇主要介绍 sessionStorage(会话存储) ...

  4. 似懂非懂的localStorage和sessionStorage

    一.区别 相信很多人都见过这两个关于HTML5的新名词!HTML5种的web storage包含两种存储方式:localStorage和sessionStorage,这两种方式存储的数据不会自动发给服 ...

  5. html5存储方式localstorage和sessionStorage

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. 区别:localStorage无时间限制,除非主动删除数据,否则永不过期:sessio ...

  6. 关于HTML5本地缓存技术LocalStorage 本地存储 和 SessionStorage

    如果你想在用户访问的时候记录或者记住他们的行为,你会想到的是什么,cookie 和session.但今天告诉你还有两种或者说是1种吧 那就是html5的 LocalStorage 本地存储和 Sess ...

  7. Cookie, LocalStorage 与 SessionStorage

    Cookie, LocalStorage 与 SessionStorage相同点 都是储存在用户本地的数据. 意义在于避免数据在浏览器和服务器间不必要地来回传递. 三者的特点     同属于html5 ...

  8. Html5浏览器缓存 sessionStorage 与 localStorage

    一.sessionStorage: 浏览关闭会话结束就被清除:(不能跨页面) localStorage:永久保存: 二.使用 var storage = window.sessionStorage; ...

  9. 【原】灵活运用sessionStorage或者localStorage

    有时,一个app中,后台并没有提供页面中对应的信息接口,需要前端在页面跳转时把某些信息带入下一个页面,一般想到用url后带参数的方法,但是有时需要带的参数过长,就不适合用这个方法了,所以用sessio ...

随机推荐

  1. MongoDB 安装 Windows XP

    〇.  一个提供MonogoDB丰富资料的中文网站 http://www.cnblogs.com/hoojo/archive/2012/02/17/2355384.html 一. http://www ...

  2. 使用Axure RP原型设计实践06,登录验证

    登录验证主要功能包括: ● 用户名错误,提示无效用户名,用户名和密码文本框清空● 用户名存在,密码错误,提示密码错误,密码清空,焦点进入密码框● 用户名和密码都正确,验证通过 本篇接着"使用 ...

  3. RichEdit控件 SDK 参考手册

    RichEdit控件 SDK 参考手册 摘要: 本文对Rich Edit控件底层消息机制进行了讲解,以期读者对Windows平台下的Rich Edit控件有一个更深入的认识,同时对于使用Win32 S ...

  4. Quartz 定时任务设置某个时间区间每隔一定时间触发的cron表达式

    原文:https://blog.csdn.net/yansong_8686/article/details/46991189 Cron表达式 Quartz使用类似于Linux下的Cron表达式定义时间 ...

  5. mqtt Qos

    mqtt Qos QoS Level 0:至多一次意思就是给你转发一次就得了,不管你有没收到.这个我理解是如果接收方离线了就不能收到消息,可以用在音视频聊天请求,因为当接收方离线后就不用收到请求了,就 ...

  6. Java实现在线预览Word,Excel,Ppt文档

    效果图:

  7. java.lang.IllegalArgumentException: No converter found for return value of type: class XXX.XXXX

    最近项目中用到fastjson做接口数据的转换,发现报这个错误了,环境是springMVC4,看到阿里的官网说是“ 如果是使用 XML 的方式配置 Spring MVC 的话,只需在 Spring M ...

  8. 关于MySQL的行转列的简单应用

    sql 脚本 -- 创建表 学生表 CREATE TABLE `student` ( `stuid` VARCHAR(16) NOT NULL COMMENT '学号', `stunm` VARCHA ...

  9. 带标签的 break 和 continue (Java)

    2002年12月发于 CCW-I2006年9月搬至 CSDN,略作修改 Java 中有 goto 关键字,但这个关键字没有任何作用,换句话说,我们不能使用 goto 来进行跳转到某行.实际上,结构化程 ...

  10. Java Run-Time Data Areas

    前言 本文主要介绍JVM的运行时数据区 来自Oracle文档 Java Virtual Machine Specification -- Chapter 2. The Structure of the ...