David Debnar
David Debnar

Reputation: 125

Loading an array from .txt file

I need to load an array from a .txt file. It has few strings separated by commas (,).

So far I have found this.

That guy splits it with .split function but I have a problem. He uses an input to upload the file, however, I need to link the file to the HTML and than split it into an array and use it.

Upvotes: 2

Views: 17980

Answers (2)

Sören Titze
Sören Titze

Reputation: 1005

You can read html files locally since html5. Check the html5 API and some examples here. Then use Alexanders answer to parse it.

EDIT: here and here are some good and short examples on how to use it.

Upvotes: 1

Alexander Støver
Alexander Støver

Reputation: 645

Just make sure the text file is accessible through an HTTP request on the same domain and you can read it fine. Here's an example where the text file is located in the webroot.

    $(function(){
        $.get('/whatever.txt', function(data){
            var array = data.split(',');
            console.log(array);
        });
    });

Upvotes: 3

Related Questions