Search code examples
javascripthtmlfileline

How to read a local text file in cordova?


I am currently working on a html5 app and for this I need to access a .txt file which is storing variables. These variables need to be accessed inside a javascript file thats stored outside the index.html.

e.g.

daysInWeek = 5

How can I read in my text file? Each variable is stored on a new line and I need the file to be read when the app loads.


Solution

  • You could place your file inside your www folder and make a http request to load it from your application. This would return the contents of the file and then you could process it the way you want. Anyway, I would use a JSON file instead of a text file.

    Example for a file located inside www/JSON, using angularjs $http service:

    $http.get('json/myfile.json').then( function(response){ 
        var data = response.data;
        //do your processing in here
    }, function(error){ 
        console.log("Error opening local resource ");
    } );