Javascript create array from local file.txt -


i have text file in same directory javascript program:

test.txt

1 2 3 4 5 

i want load data in array. @ end have array variable follow:

[1,2,3,4,5] 

you can use xmlhttprequest load text file, jquery does, can put this:

var array = []; var xmlhttp; if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari     xmlhttp = new xmlhttprequest(); } else { // code ie6, ie5     xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function() {     if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {         var text = xmlhttp.responsetext;         // convert array using regex         array = text.split(/\n|\r/g);     } } xmlhttp.open("get", "test.txt", true); xmlhttp.send(); 

this way in array form, text test.txt file.

i assuming test.txt in same folder script does


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

Kivy: Swiping (Carousel & ScreenManager) -

jdbc - Not able to establish database connection in eclipse -