Best of all, since the file on your server should receive it through "ajax". This means asynchronous JavaScript and XML, but part of XML is completely optional, it can be used with all types of content (including plain text). (In this respect, the asynchronous part is also optional, but it is best to stick with it.)
Here is a basic example of querying text file data using ajax:
function getFileFromServer(url, doneCallback) { var xhr; xhr = new XMLHttpRequest(); xhr.onreadystatechange = handleStateChange; xhr.open("GET", url, true); xhr.send(); function handleStateChange() { if (xhr.readyState === 4) { doneCallback(xhr.status == 200 ? xhr.responseText : null); } } }
You would call it like this:
getFileFromServer("path/to/file", function(text) { if (text === null) {
However, the above is somewhat simplified. It will work with modern browsers, but not with some older ones, where you will have to solve some problems.
Update . You said in a comment below that you are using jQuery. If so, you can use the ajax function and take advantage of jQuery workarounds for some browser inconsistencies:
$.ajax({ type: "GET", url: "path/to/file", success: function(text) {
Side note:
I found that javascript is client based ...
Not. It is a myth. JavaScript is just a programming language. It can be used in browsers, on servers, on your workstation, etc. In fact, JavaScript was originally designed for server-side use.
These days, the most common use (and your use case) is for web browsers, client-side, but JavaScript is not limited to the client in the general case. And actually it has a serious resurgence on the server and elsewhere.