Require some advice to get me started.
Just trying to do something simple at the moment for learning purposes.
Written a simple html page, with some javascript on a 'client'.
I need a way of getting a file list from my own webserver (including info such as date modified), that I can run from my simple page somehow.
So my question, is javascript + ajax the way I should be approaching this, or is there an easier/better way. Links to tutorials/examples greatly appreciated.
Thanks.
You need to write two scripts:
One that scans a particular directory on webserver and generates results in a format suitable for AJAX (I would recommend JSON format but not JSON-P).
An AJAX script that queries the script, formats and displays the data.
What server side scripting languages are you familiar with?
is javascript + ajax the way I should be approaching this
That would be only part of the solution. You will also need to write a server side script which will return the list of files and then you could query this script using AJAX by passing it the script name yuo would like to get info about. The reason why you might need a server side script is because javascript has no direct access of server files and information like date modified.
HTML and JavaScript run on client side. You need a server side technology to parse the file list on your web server and throw the output in HTML. Some server side technologies are ASP.NET, PHP, NodeJS etc.
Related
I'm working on a webapp that is currently running on a server, where there are also some .JSON files, which I would like the user to be able to type some information and press enter where it will be stored to the .JSON file.
The webapp is written in HTML, CSS, and vanilla Javascript, there aren't any external libraries being used.
I am already pulling information from the .JSON files to be used in the app using the GET function, and I know I can't directly modify server side files with javascript unless I'm running a Node.js server (which isn't currently an option).
So I believe my only option is to use a server side language such as PHP (which I nothing about), to modify the file. My question is, how can I do this relatively simply? Possibly when a JS function is run to push the change to the file.
Can anyone give me a sliver of example code, or point me in the direction of some simple documentation or tutorial on how to do this, I'm not very adept at server side programming at all, and as this is a simple project just for me, I don't want to dive deep into PHP at the moment.
Thank you in advanced!
So I believe my only option is to use a server side language such as PHP (which I nothing about), to modify the file.
Since you already know JavaScript, I'd revisit why you feel you can't use Node.js server-side. In any case, if you're going ahead with PHP...
To write files, file_put_contents().
To encode JSON, json_encode().
Note that there are other servers out there. You don't have to write your own stuff in PHP. If you don't need any checking on what's being sent, you can probably even modify your web server's config to accept a PUT.
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.
There are a couple of aspects of node.js I don't quite understand. I hope someone can make things clearer
When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.
Does node.js replace client side javascript?
How does node.js interact with HTML? For example if I wanted to put data from the server into this div element <div id="content"></div>
In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>
Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.
Whats confusing me is that because it is run from the server then I expect that I can use javascript on the browser to get data from the node.js server. However, examples I seen this is never done.
Thanks in advance
When you install node.js where do you store your files so that the web browser can display your content? For example Apache have a www folder.
Wherever you want. node.js doesn't serve static content, it runs JavaScript. You tell it which script to run when you start it.
You could write some JavaScript that serves static content, but where you would keep it depends on the code you wrote.
Does node.js replace client side javascript?
Only in so far as any server side programming replaces client side JavaScript.
One advantage of using JS on the server side is that you can reuse libraries on both the client and the server. See Mojito for a framework that claims to focus on this (I haven't had time to try it myself yet).
For example if I wanted to put data from the server into this div element <div id="content"></div> In PHP you could do something like this: <div id="content"><?php echo $content; ?></div>
PHP is a template language with an embedded programming language. JavaScript is a programming language. Typically you would use a template language (e.g. moustache) from your JS.
Would you ever call node.js from client side? For example: An Ajax request to node.js to get data.
Yes, if you want to. Just like any other server side programming environment. (Assuming you are using node to run an HTTP server).
Node.js is not a server (like e.g. Apache). It's a platform to run Javascript with some built in libraries (so-called modules). It is very easy to write server (HTTP or any other) in Node.js, but you can also write completely different programs (no network related, meant to be executed locally).
I suggest you read this: http://www.nodebeginner.org/. It took me several hours but allowed me to understand basics of Node without much pain.
As for client side scripting, it's generally separate. Code in Node runs in separate environment then the one in browser. They can communicate, but you have to explicitly make them to. It's not much different from server side coding in PHP. The code on server produces some output (eg. HTML) which is sent to client. If there are scripts in output, client (browser) executes it. They can communicate (via XHR, websockets, etc.), but by itself those scripts are separate.
How does node.js interact with HTML? For example if I wanted to put
data from the server into this div element In
PHP you could do something like this:
You would probably send the content as JSON to the JS-Client and insert it into the DOM (using plain JS or JQuery).
I've wrote a REALLY trivial (and not quite feature-rich :-P) chat application in Node.js a while a go, to try out some concepts and understand working with JS on both the client and server. Maybe it will give you some clues.
EDIT
In this application, the server also serves static files, which you should not do when implementing production ready application (Node is not really suited for serving static files!).
I have a website with a table that is populated with data from various external XML feeds. The table is generated using Javascript as after some reading, I found that this seemed to be the best approach for creating an HTML table from XML data (please correct me if wrong!).
I now want to parse this HTML table in to an RSS feed and I'm struggling to find the best way to do so. I have php code that will parse an HTML table, but because this table is generated using JS (ie. client side) the PHP parser does not work. Can anyone tell me the best way to go about this?
As you've probably gathered, I'm quite new to programming so layman terms would be much appreciated where possible.
Thanks a lot.
I found that this seemed to be the best approach for creating an HTML table from XML data (please correct me if wrong!).
As a rule of thumb, if instant feedback isn't required (and it isn't if you are fetching data from multiple external sources), if you can do it server side, then do it server side. You only have one server side environment to deal with instead of dozens of different client side environments (some of which could have JS turned off).
I now want to parse this HTML table in to an RSS feed and I'm struggling to find the best way to do so. I have php code that will parse an HTML table, but because this table is generated using JS (ie. client side) the PHP parser does not work. Can anyone tell me the best way to go about this?
Write PHP to get the data from wherever the JS gets its data from. You already have the logic to query it in JS, so you should be able to do a fairly straight port of that.
It is not possible to generate an RSS feed from pure JavaScript, as most RSS clients don't speak JavaScript, and the standard doesn't provide for it - you won't be able to run the commands required to create the data.
Replicate the functionality of your JavaScript aggregator using some server-side language like PHP, and build an RSS feed from it. It will require rewriting your entire code, but probably is the best way to go.
How can I do a foreach(File file in Directory) kind of thing in jQuery.
Thank you!
Javascript does not have access to the local file system for obvious security reasons. This is not possible.
Unless you are trying to loop through files on your server, in which case you wouldn't want to use jQuery anyway but something like ASP.NET or PHP or whatever framework you are using.
$('selector').each(function(idx, elm){
//some code
});
Will allow you to iterate over a list, applying the same function to each. However, accessing the filesystem is not possible.
It is not possible with javascript/jquery to read contents of a directory for security reasons. Imagine you are trying to read the file system of client machine. You can use FSO (File System Object) but that works only in IE. You can use server-side languages such as PHP, ASP.Net for accessing the file system.
If you meant a loop with each, consider the each method of though. Example:
$('selector').each(function(){
// your code.....
});
If the files are on the client you can't access them for security reasons.
If the files are on the server and you want process the list sever-side you wouldn't use JQuery as it's for client-side scripting.
If the files are on the server and you want to process the list client-side, you could generate the list in the server code and send it down as xml, json, etc.
If the files are on somebody else's server your only options is to recursivly follow links on their site, which is not really something you'd want to be doing with Javascript from a client's browser.