Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active December 26, 2015 06:09
Show Gist options
  • Save joyrexus/7105403 to your computer and use it in GitHub Desktop.
Save joyrexus/7105403 to your computer and use it in GitHub Desktop.
Load local json file
<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
<style>
#file {
margin-top: 25px;
margin-left: 50px;
font-family: "Helvetica Neue", sans-serif;
font-weight: 100;
font-size: 100px;
color: #777;
-webkit-font-smoothing: antialiased;
}
#file:hover {
color: orange;
cursor: pointer;
}
input {
display: none;
}
</style>
<body>
<input type="file" id="chooser" />
<div id="file">Click and choose</div>
<script type="text/coffeescript">
$ = (selection) -> document.querySelector selection
load = ->
File = @files[0]
return if not File.type.match '\.json$'
file.textContent = File.name
reader = new FileReader()
reader.onload = (file) ->
data = JSON.parse @result
console.log data
reader.readAsText File
chooser = $('#chooser')
chooser.addEventListener('change', load)
$('#file').addEventListener('click', -> chooser.click())
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment