javascript - webapp read text file ajax, httprequest, or -
i'm using jquery mobile create web app... know best way read text file? @ moment, have functions working fine... don't know differences between them, , 1 better use? or fastest...
function 1
function readfileajax() { $.get('txt/info.txt', function(txt) { var lines = txt.split(/\n/); var randlinenum = math.floor(math.random() * lines.length); var text = lines[randlinenum]; var parts = text.split(/#/); var fulltext = parts[0] + " " + parts[1] + " " + parts[2]; $("#msg").append("<p>" + fulltext + "</p>"); }); }
function 2
function readfilehttprequest() { var filepath = "txt/info.txt"; xmlhttp = new xmlhttprequest(); //xmlhttp.overridemimetype('text/plain'); xmlhttp.open("get",filepath,false); xmlhttp.send(null); var filecontent = xmlhttp.responsetext; var lines = filecontent.split(/\n/); var randlinenum = math.floor(math.random() * lines.length); var text = lines[randlinenum]; var parts = text.split(/#/); var fulltext = parts[0] + " " + parts[1] + " " + parts[2]; $("#msg").append("<p>" + fulltext + "</p>"); }
thanks
function1 uses jquery wraps xmlhttprequest. if have jquery it's easier use whole way.
there's no real appreciable difference between 2 functions in terms of performance though.
edit: more reason use function1, there's no reason make synchronous. 3rd argument on open set false makes request synchronous, gives no improvement , block interactivity of page. syncronous call means blocks execution until request completed. if switch true, , use callback there's no appreciable difference.
Comments
Post a Comment