3

I'm experimenting with dynamic(ish) server less sites, (no database, serversided languages, etc.) which is why I am hoping to only use JavaScript/AJAX/jQuery. I want to be able to serve a dynamic(ish) webpage with static files.

One solution I came up with is to essentially use text files (json, xml) on the same server as the web page to serve as a datastore. But I could only find ways to load the entire file rather than specific parts of it. Which isn't really a big problem since the text files are small, however the text files could potentially grow to be large.

If you could load the file line by line, the payload to the client would be faster, and the JavaScript parsing would be sped up significantly.

If I am only interested in fresh data, I was hoping I could prepend the data to the top of the file when populating the file and read it in line by line (since I would only be interested in say 10 lines per page). Also access to the middle of a file would be incredibly useful.

I just want to avoid having to send the entire file to a client(not for security but for space/time efficiency).

Is there any way to do this in JavaScript? Or am I missing an obvious solution?

3
  • If there is no server...how are you going to return only a portion of a file... I seriously doubt this is possible since you are wanting to go sans server. Why not just use a datastore that can be queried from javascript? This would allow the only "server" to be for document retrieval. couchdb.apache.org Commented Nov 6, 2012 at 22:00
  • I don't think there is a way to do this with just javascript. Commented Nov 6, 2012 at 22:00
  • I wanted to avoid server sided languages and databases since this would allow the page to be pretty portable to almost anywhere. One extremely ugly solution I'm debating on implementing is to have a linked list style file list. Load file of 10 lines, the last line will be the next file. Yeah I was afraid that was the answer though. Thanks Commented Nov 6, 2012 at 22:05

2 Answers 2

1

I haven't done this before, but you should have a chance by having a look at the Range, If-Range and Accept-Ranges HTTP headers.

I did a short test by accessing or webserver at http://www.stadt-salzburg.at/. It sends an Accept-Ranges:bytes response header for the static html file resource of the start page which means that it accepts Range-requests. Using the Firefox extension Modify Headers I then specified a request header Range:bytes=257-2048 and the server really returned only the requested part of this file.

According to the HTTP 1.1 header field specification you should also be able to determine if the file has been modified and do some other nice stuff :-) As I'm not very experienced in HTTP headers I would like to ask you to have a look yourself if you can achieve your desired result.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm this might actually be a workable solution for my specific use case. One worry of mine was that I would need to know exactly how many bytes each line would be. But I could pad each line to be X bytes long. Thanks! It's way more complex than I was hoping, but it is definitely closer than I've gotten
In my opinion this is your ONLY chance to get close to the desired result. If you have a static file you can use a well defined set of http methods to retrieve it. By default you will get the whole file so you must rely on the http header to tell the server that it should do something different. Please tell us here if you had success!
If you're using UTF-8 and set fixed line widths watch out that special characters like german umlauts are encoded using two bytes!
Just an update, I used the Range exactly like this in the get request ( xmlHttp.setRequestHeader("Range", "bytes=32-63"); ) and it worked exactly as I hoped! So yes this can be done. The only caveat is I have to standardize my lines to use the proper amount of bytes
0

I worked on this same problem a few years ago. JavaScript on its own won't cut it - someone please prove me wrong.

  1. Add data to your local text file in real-time in the normal way (append new data to the end).
  2. have a cgi app running on the local machine that can perform an OPEN, SEEK, READ, CLOSE of one record at a time.
  3. return the new/next file offset and the text of the record to the app running in javascript.
  4. the javascript remembers the next record offset - or can set it to zero to re-process the entire fle from its start.
  5. the javascript calls the cgi with the next record offset.
  6. the cgi app receives the request and does its OPEN, SEEK, READ, CLOSE. if no new data is available the read will load zero bytes and the cgi app can return the same offset and a blank record.

I hope you can see that the above steps repeat as fast as your JavaScript wants, as long as it wants.

2 Comments

Poster wanted to avoid serverside languages. :) Yes it is impossible to do that.
i know, i don't think it's possible - be nice if it were though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.