This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var itemCount = 0; // default value for label | |
try { | |
var db = openDatabase('mydb', '1.0', 'my_data', 2 * 1024 * 1024); | |
db.transaction(function (tx) { | |
tx.executeSql('CREATE TABLE IF NOT EXISTS data (id unique, text)'); | |
}); | |
} catch( error ) { | |
alert('Your browser is not supported for WebSQL - Please use Chrome or Safari'); | |
} | |
var savedData = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get the locally saved data | |
function loadSavedData(){ | |
db.transaction(function (tx) { | |
tx.executeSql('SELECT * FROM data', [], function (tx, results) { | |
console.log(results); | |
var len = results.rows.length, i; | |
for (i = 0; i < len; i++) { | |
var saveItem = { | |
"value": results.rows.item(i).text, | |
"label": results.rows.item(i).id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function saveLocally(dataToSave){ | |
db.transaction(function (tx) { // this is nasty but truncate is not available on sqllite | |
tx.executeSql('DROP TABLE data '); | |
tx.executeSql('CREATE TABLE IF NOT EXISTS data (id unique, text)'); | |
}); | |
$.each(dataToSave, function(key, val) { // loop through each item in savedData | |
if (val){ | |
db.transaction(function (tx) { | |
tx.executeSql('INSERT INTO data (id, text) VALUES (?, ?)', [val['label'], val['value']]); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html manifest="appcache.manifest"> | |
<head> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AddType text/cache-manifest .manifest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CACHE MANIFEST | |
# 2013-02-08:v1 | |
# list all resources that are to be cached | |
CACHE: | |
index.html | |
working.html | |
default.css | |
images/me.png | |
custom.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check if a new cache is available on page load. | |
window.addEventListener('load', function(e) { | |
window.applicationCache.addEventListener('updateready', function(e) { | |
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) { | |
// New update is ready. | |
window.applicationCache.swapCache(); // swap old cache with new one. | |
if (confirm('Your experience has been updated. \n Click OK to reload your page')) { | |
window.location.reload(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html manifest="filedemo.manifest"> | |
<head> | |
<meta name="viewport" content="width=device-width"> | |
<title>Filesystem API</title> | |
<link rel="stylesheet" type="text/css" href="default.css" media="all" /> | |
<script src="jquery-1.7.2.min.js"></script> | |
<script src="custom.js"></script> | |
</head> | |
<body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function kickUpFileSystem(){ | |
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; | |
try{ | |
window.webkitStorageInfo.requestQuota(typeFileSystem, 5*1024*1024 /*5MB*/, function(grantedBytes) { | |
window.requestFileSystem(typeFileSystem, grantedBytes, function(filesystem) { | |
fs = filesystem; | |
fileSystemLoaded(); | |
}, errorHandler); | |
}, function(e) { | |
console.log('Error', e); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a new file | |
function createFile(fileName){ | |
fs.root.getFile(fileName+'.txt', {create: true, exclusive: true}, function(fileEntry) { | |
displayDirectory(); // reload files and folders | |
}, function(e) { // file was not created for some reason | |
if (e.code == FileError.INVALID_MODIFICATION_ERR){ // alert most common reason for failure | |
alert('Filename already exists'); | |
} | |
}); | |
} |