IndexedDB之限制(Limits and Restrictions)和总结

欢欢欢欢 发表于 2018-3-3 13:12

Limits and Restrictions

Many of the restrictions on IndexedDB are exactly the same as those for Web Storage. First, IndexedDB databases are tied to the origin (protocol, domain, and port) of the page, so the information cannot be shared across domains. This means there is a completely separate data store for www.wrox.com as for p2p.wrox.com.

翻译:

IndexDB的许多限制和网页存储的限制很相似。首先,IndexDB数据库被绑定到页面的站点(协议,域名和端口)上了,因此信息不能被跨域名分享。这意味着www.wrox.com和p2p.wrox.com有完全独立的数据存储。

Second, there is a limit to the amount of data that can be stored per origin. The current limit in Firefox 4+ is 50MB per origin while Chrome has a limit of 5MB. Firefox for mobile has a limit of 5MB and will ask the user for permission to store more than that if the quota is exceeded.

翻译:

其次,每个站点有一个可存储的数据量限制。FF4+的当前限制是每个站点50MB,Chrome是5MB。FF移动是5MB的限制,并且如果配额已满,将会询问用户授权。

Firefox imposes an extra limitation that local files cannot access IndexedDB databases at all. Chrome doesn’t have this restriction. When running the examples from this book locally, be sure to use Chrome.

翻译:

火狐还有一个限制,本地文件完全不能访问IndexDB。Chrome没有。当运行本书实例时,确保是用Chrome。

SUMMARY 汇总(是书中本章节的汇总,所以包含了很多其他的内容)

Offline web applications and client-side data storage are a big part of the Web’s future. Browsers now have the ability to detect when a user has gone offline and fire events in JavaScript so that your application can respond. The application cache allows you to specify which files should be made available offline. A JavaScript API is available for determining what the application cache state is and how it may be changing.

翻译:

离线网页应用和客户端数据存储是未来网页的一大部分。使用JavaScript浏览器已经有检测浏览器是否离线的能力,以便让你的应用做出合适的响应。应用级缓存允许你指定哪些文件应当被离线可用。决定应用级缓存状态和缓存正如何变化有一个Javascript API。

This chapter also covered the following aspects of client-side storage:

Traditionally, such storage was limited to using cookies, small pieces of information that could be set from the client or server and transmitted along with every request.

JavaScript provides access to cookies through document.cookie.

The limitations placed on cookies make them okay for storing small amounts of data but inefficient for storing large amounts.

翻译:

本章也涵盖了如下客户端存储的概念。

传统的存储只能只用cookie,每次请求只有少量信息能在客户端和服务器端传输。

JavaScript通过对象document.cookie提供了对cookie的访问。

cookie的限制使得客户端存储少量数据还可以,但是大量数据就捉襟见肘了。

Internet Explorer provides a behavior called user data that can be applied to an element on the page as follows:

Once applied, the element can load data from a named data store and make the information accessible via the getAttribute(), setAttribute(), and removeAttribute() methods.

The data must be explicitly saved to a named data store using the save() method for it to persist between sessions.

翻译:

IE提供了一种被叫做用户数据的行为,他能像下面这样子一样,被应用到页面元素:

一旦被使用,元素能从一个命名的数据存储加载数据,然后通过方法getAttribute(),setAttribute(),removeAttribute(0让信息可访问。

数据必须通过方法save()被显示的保存到命名的数据存储来和sessions交互。

Web Storage defines two objects to save data: sessionStorage and localStorage. The former is used strictly to save data within a browser session, because the data is removed once the browser is closed. The latter is used to persist data across sessions and based on cross-domain security policies.

翻译:

页面存储定义了两个对象来保存数据:sessionStorage和localStorage。前者被严格的用来保存浏览器的会话,因为浏览器一关闭,数据就被移除。后者被用来跨会话保存数据,并且基于跨域名安全策略。

IndexedDB is a structured data storage mechanism similar to an SQL database. Instead of storing data in tables, data is stored in object stores. Object stores are created by defining a key and then adding data. Cursors are used to query object stores for particular pieces of data, and indexes may be created for faster lookups on particular properties.

翻译:

IndexedDB是一个结构化数据存储机制,和SQL数据库类似。数据被存储在对象仓库中,而不是表格中。定一个键值即可创建对象仓库,然后就可以添加数据了。光标被用来查询对象仓库中指定的数据,同时,索引被创建用于特别属性的查询。

With all of these options available, it’s possible to store a significant amount of data on the client machine using JavaScript. You should use care not to store sensitive information, because the data cache isn’t encrypted.

翻译:

以上接口,使得使用JavaScript在客户端存储大批量数据成为可能。你要谨慎存储敏感信息,因为数据缓存不加密。

IndexedDB示例:

示例一:创建数据库和检测版本号

示例二:创建对象仓库

示例三:创建光标,遍历对象

示例四:键值范围的使用

实例五:光标方向的使用

我的学习感受,虽然Professional JavaScript for Web Developers很经典,但也是2012年出版的了,IndexedDB好多方法属性都已经物是人非了。所以我将他原来的很多实例都改了。

粗略发现又一下几点:

1,方法setVersion改成了事件onupgradeneeded

2,open数据库的时候errorCode换成了error

3,光标方向改成了字符串了,不再是常量。