How can you read, write, and create ".txt" files using Javascript? - javascript

I am writing a website and I have a very basic understanding of JavaScript (JS) but a good understanding of HTML and CSS. I want to have an admin part of the website were you can edit the content of the site. I also need help on how to display these text files onto the website using JS. Thank you for your help.

No. You can't. For this you will need backend server software, like PHP.

You can do it with ActiveX objects - http://www.yaldex.com/wjscript/jsfilecreateTextFile.htm - but you're tying yourself to proprietary IE support.
Other than that, you'll need a server-side language such as PHP.

In HTML5 you can operate on local files via the File API (have a look at http://www.html5rocks.com/en/tutorials/file/dndfiles/ for a quick tutorial) but manipulating files on the server using JavaScript on its own is impossible. You need to use AJAX to send a request to a PHP (or some other server-side language) script to do it for you.

Impossible, only server-side languages

Related

Writing data to a local file using HTML5 and Javascript in Chrome

I have created an interactive quiz using html and javascript which will be run on a touchscreen at an event and I need to write the results to a local csv file (so no internet connection). It needs to write to an already existing file, so it cannot be done where the data is stored locally and a download link is generated through the browser.
How would I go about doing this? All methods I have found are either unreliable or no longer supported. The browser I am using is Chrome, so it does not need to be cross-browser compatible.
Can anybody help or point me in the right direction please?
Install a web server.
Point the browser at http://localhost.
Send the data to the server using Ajax or a form submission.
Process the data (including storing it in a file) using the server side language of your choice.
When javascript is used only in the client-side cannot write data as you want.
Follow the #Quentin recommendation about install some web server, as apache using php for instance(It is pretty straight forward!). I also recommend you to create restFull methods to do it with jquery calls from the client side, it is easy to find many examples in the internet and quicky...
If you want something more easy you could work with html post using forms in php, the most easy way to do it.

Is there any way to hide/see JavaScript file pointed by <SRC> tag?

I have developed AES algorithm in JavaScript and Java for secure communication between browser and server. And I am using that JS file with tag, now when we right click on the page we can see view source, in that view source only location of JavaScript file is visible. I am worried that whether any such tool is available that can retrieve that JavaScript file pointed by ? If yes then my key will be exposed. Is there any work around to it?
Your Javascript code executes on the client.
Therefore, the client can read and execute that code.
Client-side cryptography is (mostly) an exercise in futility.
Your system is not secure.
If I assume that your javascript src tag looks like this:
<script src="www.mydomain.com/scripts/login.js" />
Then all I need to do is put: www.mydomain.com/scripts/login.js in my address bar and I can read your JavaScript file plain as day. You might be interested in implementing some JavaScript obfuscation though. Here is a decent post: How can I obfuscate (protect) JavaScript? you can also Google 'JavaScript Obfuscation' for a multitude of information.
This is obviously security through obscurity which isn't really security at all but I suppose it is better then nothing in your case.

Can I save a file which will be generated from php code on server side using only javascript/C/C++ and no browser opened?

What is the best way to save a file from internet on javascript and/or C and/or even C++?
I saw this same question for C# and Java, but nothing to this three languages, so here is the question.
Hey, not so easy. The url point to some http://xx.xxxx.com/p.php?pid=staticetctectc....
I guess is php code which produce a nice gif in my browser. I just want to save this gif. Without opening browser. It is possible to do with javascript/C/C++?
Most near answer I found is this.
Thanks in any advice.
You can easily do this with JavaScript using Node.js. Here is a link to an example: http://www.hacksparrow.com/using-node-js-to-download-files.html
You could also do it from the command line using wget or curl. They are both available on pretty much every platform you can imagine.
For C++, you could do it like in Downloading File in Qt From URL. If you do not want to depend on Qt, libcurl is also an option.
For JavaScript, the method described in File Download Using JavaScript should work.

blogengine without php or asp.net etc

Is there a way to have a blog directly integrated into my HTML/javascript-only website, without having to have something like a SQL-database and a dynamic engine like PHP or MySQL?
Maybe there is some service in the web that offers this (hopefully without ads :) ). Or maybe I can have a blog engine entirely written in javasript?
Entirely written in JavaScript? Surely that defeats the entire point of having a "blog-engine" in the first place? The point being that the data is stored somewhere and dynamically retrieved. To avoid using anything server-side (which seems to be your intent), and only use HTML/JavaScript, you'd have to store all the data for the blog in files that are served up to each visitor, and then retrieve the data from the particular, local, locations using JavaScript.
Sorry if I'm misunderstanding the point here... but this seems to be an utterly useless way of trying to go about things. Blogs are, in general, either written statically (in HTML [even though this is rare]), or are dynamically generated from a database by a server-side scripting language (most common).
Edit: As an additional point, I suppose you could include some third-party blog feed, or service, in your page, via use of JavaScript... but I'm unsure as to which (if any) blogging services would directly support this method of working. Additionally, this is quite an unreliable way of including third-party data in a page...
Here's a thought. It's not really a blog engine - but a wiki.
Entirely javascript/html/css. All lives in a single html file:
http://www.tiddlywiki.com/
not sure how it would work on a real live site, but their site is using it:
* A personal notebook
* A GTD ("Getting Things Done") productivity tool
* A collaboration tool
* For building websites (this site is a TiddlyWiki file!)
* For rapid prototyping
* ...and much more!
You could use github pages. You will get a generated blog with version control.
Other option is to use a Desktop blog tool and then update your site.
You can user iWeb if you have a Mac or CityDesk on Windows or you may try this open source tool
Edit Today I came across this tool: Zeta producer that may help.
http://code.google.com/p/showdown-blog/
Blog engine written in just JS and XML [v0.6] {JavaScript, XML}
So, what you want is to have a blog where you're website provider doesn't provide a way to serve dynamic content?
The only way I see that you can do it in that case is writing html-files (or text-files if you prefer) and adding them to the site. After that you can have some JavaScript to add them to your "blog-page".
You of course need to upload them to the website in the same way as you do for the other files, and then have a way for the JavaScript to know which pages it should fetch.
I am not aware of any JavaScript blog-engines, but you can have a look at the templating functions in for instance Prototype
Of course, that means that you will have to fetch both the template and the content through Ajax and let the client do all the processing (could be slow and possibly insecure), and you still need to have a place to upload the content and update it.
Your best bet is going to be using a generator to create the HTML/CSS/JS to upload to your server, take a look at Webby: http://webby.rubyforge.org/
IF you really need to you can use a public api for a service that lets you post small bits of info and retrieve it using javascript.
for example if you only need small posts you can make a blog in html.javascript that utilizes twitter as the engine. of course you will be limited to 140 chars. I am sure there are other services that will allow a similar idea but with less restrictions.
And of course the best option - Get a blog software or host your blog with a service provider and link to it from you site.
Good luck
One solution would be to use some application that generates the static web pages of your blog, and uploads them to your web server. This way you'd have a blog with static content that could all be managed in javascript alongside your existing site, without needing to install database, daemon software, or additional dynamic web programming languages on your server. The static content generation could happen directly on your server if possible, or you could run the html generation tool locally and upload the output.
MoveableType has a tool like this. You still need somewhere to store the content of your blog, and for this MoveableType uses MySQL by default, so you'd still need to install a database somewhere, but the database could simply be one your local desktop.
MoveableType also has support via plugins or older versions that can retrieve data from a sqlite or other database. The advantage of sqlite is that it doesn't require installing daemons like MySQL does, you can just put a sqlite file on disk somewhere, give MoveableType the path to the file, and run the script to generate your static content.
There are likely other tools like MoveableType, and I have in the past generated blog-like web pages simply by writing small scripts to generate HTML. The main issue is just that you need somewhere for these scripts to fetch data from.
Another option might be to develop your blog using XSLT, ... with XSLT, you'd put the content of your pages in XML files, and then write a template in XSL that converts your XML to HTML.
If you google for 'static blog site generation' you might find other ideas/options, including Jekyll/github mentioned in one of the other responses.

Edit a file using javascript

I need to edit an xml file using javascript. Now I'm involved in a project of online testing.
The question.xml file is already in the project folder.
In that i want to add or edit the questions(only using javascript). I'm able to bring the particular content through ajax, but I am not able to edit the file.
Javascript can't write to a file. The best you'll be able to do is get Javascript to read and edit the XML then post that data to a server-side script to write to file.
Until now, Google Chrome is the only web browser that has a functioning implementation of the FileSystem API, therefore, it may allow you to save files locally using only Javascript.
Obviously, for security reasons, when writing files to the local file system, the user must explicitly allow it.
A working tutorial: http://www.html5rocks.com/en/tutorials/file/filesystem/
Nickf is correct. The reason Javascript can't write to a file is because it is a Client-Side language. Javascript will never have permission to write a file because it has to operate inside the browser sandbox.
You will need to use a server-side script (.NET, PHP, ColdFusion, etc) to write the file.
If you are willing to use Google Gears, you get a sandbox on the client machine on which you can write files.
Javascript has no built-in file I/O (a.k.a. you can't do it with JS alone)
Instead use some kind of server side language such as PHP or ASP.NET in conjunction with Javascript's AJAX functionality.
Look over Adobe's Flex development system. There are ways you can use it to build an app that runs in the browser (or not) and can access the filesystem (Windows/Mac/Linux). It's programmed in ActionScript, a dialect of javascript; and can interoperate with javascript in the browser.

Categories