IndexedDB之限制(Limits and Restrictions)和總結
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,光標方向改成了字符串了,不再是常量。