简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM