Skip to content

Instantly share code, notes, and snippets.

@glynrob
glynrob / gist:4746686
Created February 9, 2013 19:25
App Cache detection and swap
// 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();
}
}
@glynrob
glynrob / gist:4746666
Created February 9, 2013 19:20
Example manifest file
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
@glynrob
glynrob / gist:4746652
Created February 9, 2013 19:19
htaccess manifest type
AddType text/cache-manifest .manifest
@glynrob
glynrob / gist:4746633
Created February 9, 2013 19:14
Manifest call
<!DOCTYPE html>
<html manifest="appcache.manifest">
<head>
@glynrob
glynrob / websql-savedata
Created January 27, 2013 12:24
Save data with websql
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']]);
});
@glynrob
glynrob / gist:4648124
Created January 27, 2013 12:19
WebSQL Load saved data
// 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
@glynrob
glynrob / gist:4648098
Created January 27, 2013 12:14
WebSQL try to create database and table
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 = [];
@glynrob
glynrob / gist:4520053
Created January 12, 2013 19:19
Get current size of localStorage
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;
}
@glynrob
glynrob / gist:4519988
Created January 12, 2013 19:10
localStorage test function
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);
@glynrob
glynrob / gist:4466926
Created January 6, 2013 12:52
List webapp detect if online
// display if they are online or not
if (!navigator.onLine){
$('#status').html('Offline');
} else {
$('#status').html('Online');
}