Created
January 16, 2014 22:22
-
-
Save mwbrooks/8464659 to your computer and use it in GitHub Desktop.
Read config.json with the Web File API
This file contains 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 readFile(filepath, callback) { | |
window.requestFileSystem( | |
LocalFileSystem.PERSISTENT, | |
0, | |
function(fileSystem) { | |
fileSystem.root.getFile( | |
filepath, | |
null, | |
function gotFileEntry(fileEntry) { | |
fileEntry.file( | |
function gotFile(file){ | |
var reader = new FileReader(); | |
reader.onloadend = function(evt) { | |
callback(null, evt.target.result); // text | |
}; | |
reader.readAsText(file); | |
}, | |
function(error) { | |
callback(error); | |
} | |
); | |
}, | |
function(error) { | |
callback(error); | |
} | |
); | |
}, | |
function(error) { | |
callback(error); | |
} | |
); | |
} | |
// use it | |
readFile('config.json', function(e, text) { | |
var config = (text) ? JSON.parse(text) : {}; | |
console.log(config.URL); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
man, we REALLY need to create the Hi5 lib + @davejohnson