Firmly set the values inside the non-javascript type script tag, for example text/csv , then extract it using innerHTML or $("#unparsed").html()
<script type="text/csv" id="unparsed"> url,title,size images/address-book.png?1354486198, Address Book, 1904 KB images/front-row.png?1354486198, Front Row, 401 KB images/google-pokemon.png?1354486198, Google Pokémon, 12875 KB ... </script> $(function parseData(){ $("#file").hide(); var data = $("#unparsed").html(); var parsed = $.parse(data); $("#parsed").val(JSON.stringify(parsed)); })
http://jsbin.com/racanefi/10/edit
Rigidly set the values inside the text box.
$(function parseData(){ $("#file").hide(); var data = $("#unparsed").val(); var parsed = $.parse(data); $("#parsed").val(JSON.stringify(parsed)); })
http://jsbin.com/racanefi/8/edit
OR
Store the value in localStorage.
var storage = localStorage; var key = 'unparsed_text_file'; function getFile(){ $("#file").change(function(){ var file = $(this).eq(0)[0].files[0], reader = new FileReader(); reader.onload = function(e) { var text = reader.result; storage.setItem(key,text); parseData(); }; reader.readAsText(file); }); } function parseData(){ $("#file").hide(); var data = storage.getItem(key); var parsed = $.parse(data); $("#unparsed").val(data); $("#parsed").val(JSON.stringify(parsed)); } if(storage.getItem(key)) parseData(); else getFile();
http://jsbin.com/racanefi/6/edit
Browser Compatibility: http://caniuse.com/namevalue-storage
This is a rough project, you should probably verify it correctly before implementing it.
edit: I had this back; sessionStorage is temporary for sessions. localStorage is more persistent. I created a variable that allows you to assign var storage
Shanimal
source share