Write json to local disk javascript - javascript

I wrote a small application(proof of concept) in angular.js and i encountered the need of persisting simple data between application instantiations.
I would like to know if there's a way of storing JSON objects to a local file, on my computer, without server or database intervention.
The easiest solution I found is using ColdFusion, but this requires setting up a server too.
Thanks!

You can use HTML5 Local Storage.
you can store Json object like this:
var jsonObejct = {'city':'New Yourk', 'country':'USA'}
localStorage.setItem('jsonObejct',JSON.stringify(jsonObejct));
you can retrieve from Storage Like:
var loadedObj = localStorage.getItem('jsonObejct');

Related

Full CRUD app without a database possible?

If I use a json file stored in one of my GitHub repos as a mock backend, I know how to fetch and read all the data. Is it also possible to edit or post new data to this json file? Would an alternative mock backend like Mocky.io be a better solution (to achieve full CRUD)?
I think you could store the information inside csv files or somehting like that, you would be recreating a database engine as MongoDB & create your own reader to find the info, or you could store the users info using local Storage,
However this would make your app very limited.
Here's the link for the documentation of local storage
https://developer.mozilla.org/es/docs/Web/API/Window/localStorage
Well if you want to try out CRUD operations you can use free JSON APIs like http://jsonplaceholder.typicode.com/ or
https://mockfirst.com/
where you can create, read, update and delete data using various api end points. It is better to go this way first then you could move on to updating a JSON file.
(UPDATE)
You can use https://jsonbin.io/
Here you can place your own data and use it as an API.

Using PHP - is it possible to set SessionStorage in the browser?

Im pulling in a $_SESSION variable coming from my database, either: $_SESSION['lvl'].
After the level is fetched, I'd like to store that variable in the browser's session as key value pairs using the sessionStorage API; is this possible?
I do not see any docs or info as it looks like this is only possible using JavaScript.
I'd like to set it within my PHP service.
Any advice is appreciated.
Well, you need to produce Javascript code that will do it on the client, and it's gonna be a pain to get the value on the server side.

Store data locally using HTML and JavaScript

I have a small LAN with some computers.
I'm looking for a way to build a dynamic HTML webpage that uses JavaScript to store some data locally (can't use server side - only client side).
The webpage will be stored on a network drive shared with all the computers.
I wish to do that using a file, maybe an XML file or something similar that will be loaded using JavaScript and then saved again after some changes.
The data must be shared with all the computers on the LAN.
How can I do this?
HTML5 localStorage
//Set
localStorage.setItem("lastname", "Smith");
//Get
var lastName = localStorage.getItem("lastname");
You have the following options :
1.LocalStorage : You can store data in variables. There would be a limit as to how much data you can store.
Refer : http://en.wikipedia.org/wiki/Web_storage.
Eg:
// Store
localStorage.setItem("sample", "test");
// Retrieve
var sample = localStorage.getItem("sample");
2.WebSQL : This should be the most easy way of storing in client side. WebSQL is supported in almost all current browsers(HTML5). However there is no longer official support for WebSQL as its depreciated and no future updates.
Refer : http://en.wikipedia.org/wiki/Web_SQL_Database
3.IndexedDB : This is also another way to store data in local database. However this is not yet supported in all browsers.
4.XML
If you are going forward with storing data in local DB, then you can utilize PersistenceJS.
Refer : https://github.com/zefhemel/persistencejs
Try like this:
store a value :
localStorage.setItem("lastname", "amir");
get a value:
localStorage.getItem("lastname");
You can use HTML 5 LocalStorage
Depending on the flavor of data you're storing, simple cookies might work, accessed with plain old JS.
finally I found a solution for it!
I am using a jQuery plugin called: twFile (http://jquery.tiddlywiki.org/twFile.html).
It uses an activeX FileSystemObject - it works great on IE9.

How do I write data to a file with Javascript?

I made a game with javascript using this tutorial as a base: http://html5gamedev.samlancashire.com/making-a-simple-html5-canvas-game-part-3-drawing-images/
How do I get it to write the data from the item counter (var itemCounter = 50;) to a text file named savedata.txt? I googled it, but no helpful results came up. Can someone help me?
Technically, you can create a server with nodejs [which is built with javascript]. Details can be found here
Its not possible to store the data as a file on the client.
But you can use localstorage, websql, indexeddb or simply cookies for it.
Note that all of these storages have different properties in terms of lifetime.
You could also create a blob using the blobapi and then create a dataurl and request the user to save it, using drag and drop + fileapi to read the data, this approach however will make it easy for users to modify the data.
Writing a file is posible with the new FileWriter and FileSystem APIs.
More mature solutions (not using files) have already been mentioned
Javascript does not support working with files, for data storage several options are available:
cookies
Local Storage
Server side storage

How can I create a client-side database that persist between sessions?

I'm working with HTML5 to create a client-side database using the Lawnchair Javascript library, but when y create a new Lawnchair object what i get is a new local storage, not a new database
var people = new Lawnchair('people');
the problem is that in local storage I have just one table, and i need to be able to create more than just one table.
You can ref to this page for local database usage.But not all browser support it yet.
http://blog.darkcrimson.com/2010/05/local-databases/
You might want to use Web SQL Database, but that's implemented only by Chrome, Safari, and Opera. If that is not a problem look here: http://www.html5rocks.com/tutorials/offline/storage/
By the way what do you mean with
in local storage I have just one table
?
You could use the local storage to hold different values, like so:
localStorage['foo'] = "foo";
localStorage['bar'] = "bar";
localStorage['baz'] = "baz";
Values are strings, so you can also save say json or something else if you like.

Categories

Resources