Augmenting DOM Storage with IE's userData behavior
http://www.javascriptkit.com/javatutors/domstorage2.shtml
Augmenting DOM Storage with IE's userData behavior
As mentioned on the previous page, when it comes to IE, DOM Storage (sessionStorage and localStorage) is supported in IE8+. This leaves IE7 and IE6, still a formidable force on the internet depending on which survey you refer to, out in the cold. Depending on the task and your intended audience, that may be unacceptable. Fortunately, IE does actually support a proprietary feature that acts similarly to DOM Storage in terms of storage space, and that's IE's userData Behavior. Used in conjunction with DOM Storage, you can extend the guest list to your mega storage area to the lesser IE versions as well. IE userData behavior is supported in IE5+, and lets you persist up to 1MB of data per domain. Granted it's not quite the capacity of DOM Storage (4MB), but certainly a huge improvement over the miserly 4KB of space offered by JavaScript cookies.
IE's userData behaviour works as follows- you give the element in which you wish its data to be persisted a userData behavior (using either inline or external CSS), then call the appropriate userData methods of the element to save/retrieve that info:
| Methods | Description |
|---|---|
| getAttribute(attr) | Returns the persisted value stored inside the specified attribute of an element. |
| load(name) | Loads an object containing the persisted values of a particular userData behavior. |
| removeAttribute(attr) | Removes the specified attribute from an element and its persisted value |
| save(name) | Saves an object containing the persisted values of a particular userData behavior. |
| setAttribute(attr, value) | Stores a value to be persisted inside the specified attribute of an element. |
| properties | |
| expires | Gets or sets the expiration date of the data persisted with the userData behavior. |
| XMLDocument | Returns a reference to the XML Document of the persisted object. |
The best way to wrap your head around userData is just to see it in action- the following recreates the example on the previous page of persisting contents typed into a TEXTAREA in the event of an accidental browser refresh so it works in IE6 and IE7 as well:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<form><textarea id="feedback" style="width:350px;height:200px" style="behavior:url('#default#userData')"></textarea><br /><input type="button" value="Click to Hard Reload Page" onClick="location.reload(true)" /></form><script type="text/javascript">var feedbackarea=document.getElementById("feedback")if (window.sessionStorage){ if (sessionStorage.feedbackdata){ //if there is data stored for the target textarea feedbackarea.value=sessionStorage.feedbackdata //recall it upon refresh or crash } feedbackarea.onkeyup=function(e){ sessionStorage.feedbackdata=this.value //save contents of textarea as user types }}else if (feedbackarea.addBehavior){ feedbackarea.load("feedbackpersist") //load data if any persisted inside userData object "feedbackpersist" if (feedbackarea.getAttribute("feedbackattribute")){ //if there is persisted data founded inside attribute "feedbackattribute" feedbackarea.value=feedbackarea.getAttribute("feedbackattribute") //recall it upon refresh or crash } feedbackarea.onkeyup=function(e){ feedbackarea.setAttribute("feedbackattribute", this.value) //save contents of textarea into attribute "feedbackattribute" as user types feedbackarea.save("feedbackpersist") //save state of feedback textarea into userData object "feedbackpersist" }}</script> |
Demo (FF3.5+, Chrome4+, Safari4+, Opera10.5+, or IE6+):
Lets go through the important points:
- The element you wish to store userData to must be given a CSS Behavior, whether via inline or external CSS:
behavior:url('#default#userData') - To check whether the browser supports the userData Behavior, you can run a check for the
addBehavior()method on an element (ie:if (feedbackarea.addBehavior){...}) - Call
element.load(name)first when you wish to access any information stored inside one of the element's attributes. - Call
element.getAttribute(attr)to actually access the information inside a given attribute. - Call
element.setAttribute(attr, value)to store and persist any value you wish as an attribute of a given element. - Call
element.save(name)at the end after you have saved all information into any attributes of the element to actually save this information across sessions.
Note that in the above example, the contents persisted using userData lasts indefinitely, unlike the sessionStorage portion, which is just, naturally, session only. For the former, you can modify its expires property to change the duration of the persistence.
- Going beyond cookies- Using DOM sessionStorage and localStorage to persist larger amounts of info
- Augmenting DOM Storage with IE's userData Behavior
Augmenting DOM Storage with IE's userData behavior的更多相关文章
- 浅谈 HTML5 的 DOM Storage 机制 (转)
在开发 Web 应用时,开发者有时需要在本地存储数据.当前浏览器支持 cookie 存储,但其大小有 4KB 的限制.这对于一些 Ajax 应用来说是不够的.更多的存储空间需要浏览器本身或是插件的支持 ...
- userData Behavior
https://msdn.microsoft.com/zh-cn/vstudio/ms531424 userData Behavior This topic documents a feature o ...
- web离线应用--dom storage
web离线应用--dom storage dom storage是html5添加的新功能,其实也不是什么新的应用,只不过是cookie的放大版本,由于cookie的大小只有4kb,而且在每次请求一个新 ...
- HTML5在线状态检测和DOM Storage
除了离线资源缓存外,html5离线应用开发还可能用到以下技术 在线状态检测 navigator.onLine navigator.onLine 属性表示当前是否在线.如果为 true, 表示在线:如果 ...
- IE中的userData
之前做项目时用到了localstorage,但是考虑到浏览器存在IE8以下不兼容问题,所以来介绍以下IE中的userData. 本地存储解决方案很多,比如Flash SharedObject.Goog ...
- JavaScript本地存储实践(html5的localStorage和ie的userData)
http://www.css88.com/archives/3717 JavaScript本地存储实践(html5的localStorage和ie的userData) 发表于 2011年06月11日 ...
- 浅析Web数据存储-Cookie、UserData、SessionStorage、WebSqlDatabase
Cookie 它是标准的客户端浏览器状态保存方式,可能在浏览器诞生不久就有Cookie了,为什么需要Cookie 这个东东?由于HTTP协议没有状态,所以需要一个标志/存储来记录客户浏览器当前的状态, ...
- userData IE
蛮讨厌IE的,因为他常常需要特别照顾,就像DOM Storage(sessionStorage和localStorage)只能支持IE8+,对于以下的只能使用userData. 原理:通过在docum ...
- 阿伦学习html5 之 Local Storage (本地储存)
一.浏览器存储的发展历程 本地存储解决方案很多,比如Flash SharedObject.Google Gears.Cookie.DOM Storage.User Data.window.name.S ...
随机推荐
- Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)
题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...
- 日志模块logging介绍
一.日志的级别 日志一般分为5个级别,分别如下: CRITICAL = 50 #FATAL = CRITICAL ERROR = 40 WARNING = 30 #WARN = WARNING INF ...
- UWP Xaml设计器中输入特殊字符
<TextBox Text="欢迎使用小冰科技最新研发的自然语言处理程序.小冰科技旗下还有强大的人脸识别软件——<微识别>,自动追踪和识别人脸:具有科普性质的.清新脱俗的识 ...
- 淡雅清新教师求职简历免费word模板
12款精美淡雅清新教师求职简历免费word模板,也可用于其他专业和职业,个人免费简历模板,个人简历表免费,个人简历表格. 声明:该简历模板仅用于个人欣赏使用,请勿用于商业用途,谢谢. 下载地址:百度网 ...
- 【Unity Shader】(九) ------ 高级纹理之渲染纹理及镜子与玻璃效果的实现
笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Shader](三) ----- ...
- 4星|《财经》2018年第10期:远程视界自我定位为“专科远程医疗联合体O2O平台”,主要盈利模式就是做融资租赁
<财经>2018年第10期 总第527期 旬刊 本期主要内容:做远程医疗资金链断裂:人工智能时代有可能让刘易斯观点论失败:小米的盈利模式刨析:陆奇在百度的改革.其中1.4都成了朋友圈热文. ...
- Geatpy遗传算法在曲线寻优上的初步探究
园子里关于遗传算法的教案不少,但基于geatpy框架的并未多见,故分享此文以作参考,还望广大园友多多指教! Geatpy出自三所名校联合团队之手,是遗传算法领域的权威框架(python),其效率之高. ...
- c++虚继承与虚函数
学习继承与多态时看到这两个概念,记录整理. 虚继承与虚函数都是用virtual关键字实现,虚继承为了防止多重继承,而虚函数为了实现多态. 是几个例子. 虚继承: class A{}; class B: ...
- Daily Scrumming* 2015.10.28(Day 9)
一.总体情况总结 今日项目总结: 1.前后端同一了API设计以及API权限认证.用户状态保存的开发方案 2.API以及后端模型已经开始开发,前端UEditor开始学习,本周任务有良好的起步 3.前后端 ...
- 针对网站的UI分析
PM对项目所有功能的把握,特别是UI 最差的UI,体现了团队的组织架构.其次,体现了产品的内部结构.最好,体现了用户的自然需求. 对于几种浏览器分别进行UI分析, (1)360的界面如今看来比较大众化 ...