Ben Harrison
Ben Harrison

Reputation: 11

use a .js file to avoid 404 error

I have a webpage, which displays plots/info using .csv files. New files are added over time, and some go away. I need the webpage to change dynamically according to which files exist.

$.get('dir/'+myvar[1]+'.csv').done(function() {$('#Option1').show()});
$.get('dir/'+myvar[2]+'.csv').done(function() {$('#Option2').show()});

Then once the option is clicked on/selected, the $.get request is fired again and the file is actually used.

The issue is, there are a LOT options, hence alot of these requests over time. Each one throws a 404 error if the option wasn't available, and eventually I think it's slowing the webpage response down.

I was thinking of creating a .js file, which would effectively be a list of all viable options/files. The process creating the files can build the .js file each time a file is added/removed. Load the .js file on $(document).ready avoiding needless $.get requests and 404's.

The big question is, is this a viable/best practice solution, and is there an obviously better way to achieve this than I know? I am genuinely sorry if this question is too vague. Thanks.

Upvotes: 0

Views: 67

Answers (1)

Alec Fenichel
Alec Fenichel

Reputation: 1317

Receiving a 404 for an ajax request should not slow down the webpage.

However, building a file that lists the available CSVs makes a lot of sense. I would consider using a JSON file (.json) instead of a Javascript file (.js).

Depending on your webserver, this functionality might be build in. For NGINX it is: http://nginx.org/en/docs/http/ngx_http_autoindex_module.html.

Upvotes: 2

Related Questions