ユーザーデータや設定等
if (!localStorage.getItem("checkins")) {
localStorage.setItem("checkins", JSON.stringify([]));
}setItem() getItem() removeItem() clear()
http://example.com:80/
\ \ \_ port
\ \_ domain
\_ scheme
インデックスを取得 ( index ):
// db.createObjectStore("Friend", "id", true);
db.createIndex("FriendNames", "name", false);
var index = db.openIndex('FriendNames');
var id = index.get('Eric');
イテレート ( cursor ):
// name の始まりを A-E に制限
var range = new KeyRange.bound('A', 'E');
var cursor = index.openObjectCursor(range);
while (cursor.continue()) {
console.log(cursor.value.name);
}
var idbRequest = window.indexedDB.open('Database Name');
idbRequest.onsuccess = function(event) {
var db = event.srcElement.result;
var transaction = db.transaction([], IDBTransaction.READ_ONLY);
var curRequest = transaction.objectStore('ObjectStore Name').openCursor();
curRequest.onsuccess = ...;
};
HTML や CSS, JS, 画像等
<html manifest="example.appcache">... </html>
CACHE MANIFEST # 2010-11-17-v0.0.1 # 明示的にキャッシュされるエントリ CACHE: index.html stylesheet.css images/logo.png # ユーザーがオフラインの場合は static.html が表示される FALLBACK: / /static.html # ユーザーがオンラインの必要があるリソース NETWORK: *
text/cache-manifest
ユーザーデータのうちバイナリのもの
window.requestFileSystem( TEMPORARY, // 永続的か、一時的か 1024 * 1024, // 必要な領域サイズ (バイト) initFs, // 成功時のコールバック opt_errorHandler // エラー時のコールバック (オプション) );
data: )blob: )filesystem: ) New!var img = document.createElement('img');
// filesystem:http://example.com/temporary/myfile.png
img.src = fileEntry.toURL();
document.body.appendChild(img);
Filesystem URL からファイルを取得する:
window.resolveLocalFileSystemURL(img.src, function(fileEntry) { ... });
document.querySelector('#terminal').ondrop = function(e) {
var files = e.dataTransfer.files;
window.requestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
Array.prototype.slice.call(files || [], 0).forEach(function(file, i) {
fs.root.getFile(file.name, {create: true, exclusive: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.write(f); // write() は File | Blob を引数に取ることができる
}, errorHandler);
}, errorHandler);
});
}, errorHandler);
};

サーバーと直接通信する代わりに、オンライン時に通信を肩代わりし、変更点をローカルに保存してくれる Worker を用意し、これとやりとりをする。
この Shared Worker により:
navigator.onLine - 通信状況の確認if (navigator.onLine) {
console.log('ONLINE!');
} else {
console.log('Connection flaky');
}
window.addEventListener('online', function(e) {
// サーバーとデータを再同期
}, false);
window.addEventListener('offline', function(e) {
// サーバーとのやり取りをキューに追加
}, false);
| デフォルト (Temporary) | リクエストされた Quota (Persistent) | |
|---|---|---|
| Web Storage | 5Mb | N/A |
| App Cache | 利用可能なディスク容量の 10% | |
| IndexedDB | ||
| WebSQL | ||
| File System API | 任意 |
// 状況を問い合わせる
webkitStorageInfo.queryUsageAndQuota(
webkitStorageInfo.TEMPORARY, // or PERSISTENT
usageCallback,
errorCallback);
// クォータをリクエストする
webkitStorageInfo.requestQuota(
webkitStorageInfo.PERSISTENT
newQuotaInBytes,
quotaCallback,
errorCallback);
manifest.json ファイルにパーミッション "unlimitedStorage" を追加することで、クォータを無制限にすることができます。
appmator.appspot.com を利用して、簡単にアプリをパッケージ化できます。
![]() |
![]() |
![]() |
| ![]() |
|
|---|---|---|---|---|---|
| Web Storage | Y | Y | Y | Y | Y (8+) |
| IndexedDB | Y | N | Y | N | N |
| WebSQL | N | Y | Y | Y | N |
| App Cache | Y | Y | Y | Y | N |
| File System API | N | N | Y | N | N |
![]() |
![]() |
![]() |
| ![]() |
|
|---|---|---|---|---|---|
| Web Storage | Y | Y | Y (2+) | Y | Y |
| IndexedDB | Y | N | N | N | N |
| WebSQL | N | Y | Y (2+) | Y | N |
| App Cache | Y | Y | Y (2.1+) | Y | N |
| File System API | N | N | Y (3+) | N | N |
Web Storage (LocalStorage と SessionStorage)
Questions?