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
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
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
<!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
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
// 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
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
function sizeofAllStorage(){ // provide the size in bytes of the data currently stored | |
var size = 0; | |
for (i=0; i<=localStorage.length-1; i++) | |
{ | |
key = localStorage.key(i); | |
size += lengthInUtf8Bytes(localStorage.getItem(key)); | |
} | |
return size; | |
} |
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 runStorageTest(){ // fill up all data in local storage to see how much we can squeeze in | |
localStorage.clear(); | |
var i = 0; | |
var testPacket = new Array( 1025 ).join( "a" ); // create 1024 characters so 1KB | |
while (i<maxMBToTest){ // MB level | |
$('#currentDisplay').html(i+'Mb'); | |
var t = 0; | |
while (t<1025){ // KB level | |
try { | |
localStorage.setItem(i+"|"+t, testPacket); |
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
// display if they are online or not | |
if (!navigator.onLine){ | |
$('#status').html('Offline'); | |
} else { | |
$('#status').html('Online'); | |
} |