Sunday 15 September 2013

javascript - jQuery get a random line from text file -



javascript - jQuery get a random line from text file -

i have .txt file, around 754 lines. each line has pattern this:

1st part text#2nd part text#3rd part text or number

the hashtag delimiter!

what i'm trying get() or read() text file, , retrieve random line it, pattern. has random line. can print or append html div. should this:

1st part line 2nd part same line 3rd part same line

it has pattern, because maybe i'll have retrieve 1 part of pattern.

i don't know if text file right type of file pull info from.... 80kb. there way this? give me hand? i'm designer, thing driving me crazy.

what got... trying count how many lines txt have, can random line. doesn't work. text file must have \n on end of each line?

$.get('txt/messages.txt', function(txt) { var lines = txt.responsetext.split("\n"); (var = 0, len = lines.length; < len; i++) { save(lines[i]); } });

i'm using jquery 1.11.1, , jquery mobile 1.4.4

this illustration of how pull out random line txt response:

$.get('txt/messages.txt', function(txt) { var lines = txt.responsetext.split("\n"); var randlinenum = math.floor(math.random() * lines.length); save(lines[randlinenum]); // random line text file });

from there can split line based on delimeter (#):

lines[randlinenum].split("#");

here's jsfiddleexample. hope helps!

javascript jquery ajax random

No comments:

Post a Comment