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. 异步接收MSMQ消息

    在这部分,我们将使用ThreadPool 和MSMQ 进行消息收发.MSMQ 是一个分布式队列,通过MSMQ 一个应用程序可以异步地与另外一个应用程序通信. 在一个典型的场景中,我们要向维护一个队列的 ...

  2. SGU 200. Cracking RSA (高斯消元求自由变元个数)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=200 200. Cracking RSA time limit per test: ...

  3. Bootstrap datetimepicker “dp.change” 时间/日期 选择事件

    $('#<!--{$inputId}-->').datetimepicker({ todayHighlight: true, format: "YYYY-MM-DD<!-- ...

  4. android adb命令 unable to connect to 192.168.1.155:5555

    如果使用有线网络无法用adb connect命令连接设备的话,可以选择使用无线wifi来连接. 首先在android设备上装一个叫做Adb Wireless的软件,打开wifi,然后打开adb wir ...

  5. Android的学习之路(三)项目的启动过程和安装过程具体解释

    应用的安装和启动过程: 安装:第一步:java的编译器会把这个.java文件编译成.class文件           第二部:Android的SDK提供了一个dx工具,这个工具把.class文件转义 ...

  6. Auto Layout on iOS Versions prior to 6.0

    使用XCODE5.0,出现这个小错误... 解决办法: 选中你的XIB或storyboard,如下图 再查看右边属性栏 去掉最下边的Use Autolayout ,完成. 转:http://blog. ...

  7. tomcat8.0.15+spring4.1.2的集群下共享WebSocketSession?

    环境:nginx+Tomcat服务器 A B C   问题:如果用户 1 访问由服务器 A socket服务  ,用户2 由服务器 C socket服务  ,此时如果用户 1, 2 想通过  sock ...

  8. 美国谍梦第三至五季/全集The Americans迅雷下载

    本季看点:冷战间谍题材美剧,FX电视台的<美国谍梦>老派谍战剧第二季开场吸引了190万的观众.在18-49岁的目标观众群中,这部福克斯电视工作室出品的剧集有160万的收视,提高了81%.一 ...

  9. mac下设置eclipse自动提示

    偏好设置  -> General -> Editors -> File Associations .xml ->  设置XML Editor为default Keys 中查找 ...

  10. IP的准确性

    最近游戏项目中更新机制有所修改,游戏启动时会从cdn上读取一个文件(约60B),但是后台异常收集系统中发现很多玩家请求不了该文件(libcurl的get请求),返回的error code有很多种,以6 ...