How can I use session specific data files in javascript - javascript

I am creating a web application using Ruby Sinatra. However, I think this question is more related to html/javascript.
In the front end I am using d3.js for some pages. I want to create and use data files based on user input. So for each user session I want to create a data file for that session.
I think I can create the custom html/javascript for each request.
Is there any other way to do this, such as passing the file name to the javascript?

You can use sessionStorage (some examples is here http://www.nczonline.net/blog/2009/07/21/introduction-to-sessionstorage/)

Related

Is it possible to write data in JSON file without AJAX or backend?

I´m developing an small test app and I had a doubt about JSON in frontend.
I´m working with React and I´m using a JSON file as a small testing DB, saving only 1 user. The fact is that I want to write more users there and edit the JSON registers.
Is that possible without backend or AJAX?
**EDIT: **
I´m using localStorage for the actual logged in user and it´s moves, at the end need to save it´s data before I close session and localStorage loses.
AJAX is used for HTTP requests between a front-end (ex: ReactJS, VueJS) and a back-end (ex: NodeJS/Express, Python/Flask).
If you're using a JSON variable, the data will be deleted everytime the website refreshes. If you're using a JSON file, you can't access the file system from the browser for security reasons.
What I recommend to use is localStorage, which acts as a local database for front-end ONLY per browser (per user).
The most common usage is:
The setter:
localStorage.setItem('name', 'John')
The getter:
const name = localStorage.getItem('name');
console.log(name) // John
In React, you could set and update data as the following in this example I made:
DEMO https://stackblitz.com/edit/react-fctts3
Short answer, no. you can't write data directly to client's disk.
but there are workarounds.
one way is using common browsers storage options. such as localstorage:
localStorage.setItem(keyname, value)
read more here
another way if you need to move the data is to make the user download your file, and save it. when you need it again they can re-upload it to your front-end page. you can read up on this here.
of course there are other options like cookies and IndexedDB/WebSQL. but I think local storage can do just fine for your needs.
EDIT:
if you don't have problem with having an internet connection, there is also the awesom option of using google sheets as a database! using this tool:
https://sheety.co/

Creating and editing .text file from javascript

I am making a web site that makes a record of the person who visit it
I have created a html form page that requests the name from the user
But I am unable to figure out how to keep record of it,maybe something like using making a text file and having javascript add names to it,but I am not sure how to do it
I am using Google drive and drv.tw to upload the site so I can't use any server side scripting language
Native JavaScript has not access to user's file system. But Node.js has, try this one if u can. In native JS you can use something like localStorage. But this solution only for your task, do not use this for production...

Javascript - how to directly interact with db?

I have an excel sheet, a JS application and a db. excel sheet has data. I need to open the file and read data from the JS application and insert it into the db(say oracle db). How to?
I think we cannot open and close file in JS, pls correct me if i am wrong since it poses a security issue. If that is the case, say if we have updated the data in the grid, or table in the JS application. and we want to insert all the data in to the db, how do we do it?
First- there is pretty good module to read/write excel files in javascript-client or javascript-server:
js-xlsx
Someone has used it and provided a blog on its usage here
Second- You have to involve one more tier (server) to get the work done as per security standards. Or another option is BaaS (Back-end-as-Service) like Parse, Firebase to serve your client as database, it lets you directly save your stuff without involving/writing server. Or other option is to use SaaS like Mongolab, it exposes api for client to directly save into db.
Happy Helping!
You cannot do this safely. You need a webservice that the javascript app can call and that service would write to the database. So you should build a restful web api.
You need a Proxy Service for interacting to DB. There is no db-connectors for JS. The simplest way to achieve your goal is to choose one of the scripting languages which can run on your server. Such as PHP, Python, ASP.NET, ASP etc. I suggest PHP or Python.

PhoneGap (web mobile app) - Transfer data between pages

I'm developing a PhoneGap application. If you don't know what that is, it's a service that allows you to build mobile-based applications using HTML, CSS and JavaScript.
I have an application, and I've come to a point where I need to transfer information from one page to another. The user chooses an option on the page, the application then loads a new page and based on their option it loads specific content.
Now I do already know a few ways of doing this, to name one.. local storage maybe. My question is, what is the most efficient way of getting information between these two pages? If this were PHP I'd simply using $_GET[''].. but it's not PHP, I'm looking for the best way to do this using HTML, CSS, and JavaScript.
Any help here would be highly appreciated!
There are several possibilities:
You are using a service like Phonegap:build or Cordova: You only gonna have one HTML-File where you continously hide and show the different pages. I don't recommend this one, but Phonegap:build and Cordova are great to create a package for all major phones, without headache.
URL-Parameters you could pass parameter over the URL to a different HTML-Page. This means you have to open all links with window.location.replace() or change the href-attribute on you anchors. In the next page you have to parse that URL, which is not as easy as in PHP.
localStorage / sessionStorage you can easily store data in the sessionStorage. With sessionStorage.myObject = JSON.stringify(complexObject) you can easily store complex objects to the session storage and read them back with var complexObject = JSON.parse(sessionStorage.myObject). Those are available during you complete session and would be one of the easiest solutions so far.

Reading and writing to an external file using javascript?

I have an online storage account that I`m using for my homepage. Basically I have just made an "index.html" and stored there . and no php , asp is possible .
So If I must create a message form on the homepage and store the message in a separate text file in JSON format ,can it be done using javascript ?
also I need to query the Text file whenver I want to display the messages using javascript .
So far , I tried TaffyDB but realised it doesn`t have a way to persist the data after session closes. or maybe I missed something?
Thanks!
Short answer. No.
The JavaScript is client side. So it can do all sorts of cool stuff on the persons computer that visits your site but unless you're running some server side code that takes the JSON encoded data and does something with it then you're out of luck.
There are many alternatives.
If you don't want to run your own server side code then you could use a separate service like Parse.com that does REST and has a comprehensive API.
A mobile website can access Parse data from Javascript.
A webserver can show data from Parse on a website.
You can upload large amounts of data that will later be consumed in a mobile app.
You can download recent data to run your own custom analytics.
Applications written in any programming language can interact with data on Parse.
You can export all of your data if you no longer want to use Parse.
You can try with jQuery/AJAX. To read:
$.get("path_to_file", null, function(fileData) {
alert(fileData);
/* Your code goes here */
}, "text");
But in order to write, I think the only way is with some server-side language (PHP, ASP, etc)
The short answer is no.
You need to have some server-side support to persist the data on that server. You can, however, use client-side javascript to relay the information to a server that DOES support reading and writing of the data of course.
Technically, node.js is javascript that does support file reading and writing - but I assume that's out of the question for your environment :)
One crazy way (just as a thought experiment) to implement persistent storage for your web application without server side support is to have the clients talk to each other through P2P. This is possible with Flash or some java applet..etc. So as long as one client is up (perhaps your own comupter!), you'll have some form of persistent storage. Your server/webpage simply serves up this embedded object which does the actual work.

Categories

Resources