Skip to content

Instantly share code, notes, and snippets.

@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: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 / 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:4746633
Created February 9, 2013 19:14
Manifest call
<!DOCTYPE html>
<html manifest="appcache.manifest">
<head>
@glynrob
glynrob / gist:4746652
Created February 9, 2013 19:19
htaccess manifest type
AddType text/cache-manifest .manifest
@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: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:5021077
Created February 23, 2013 19:51
filesystem api html code
<!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>
@glynrob
glynrob / gist:5021087
Last active December 14, 2015 03:29
filesystem api start
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);
@glynrob
glynrob / gist:5021101
Created February 23, 2013 19:59
filesystem api create file or folder
// 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');
}
});
}