admin管理员组

文章数量:1313760

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

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.

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

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.

Share Improve this question edited May 23, 2017 at 11:43 CommunityBot 11 silver badge asked Feb 3, 2012 at 10:52 David DebnarDavid Debnar 1251 gold badge3 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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);
        });
    });

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.

本文标签: javascriptLoading an array from txt fileStack Overflow