javascript - Need help to improve this largest common substring implementation -



javascript - Need help to improve this largest common substring implementation -

i have scanned web largest substring implementations utilize in xmlhttp request, found 1 has worked, in other cases responsetext hasn't been treated string no matter have written:

txt = txt + ""; // or txt = new string(txt);)

this function works, terrible slow. wondering if code gurus out there help me improve algorithm.

the site i'm calling xmlhttprequest looking this

<!doctype html public "-//w3c//dtd html 3.2 final//en"> <html> <head> <title>index of /</title> </head> <body> <h1>index of /</h1> <ul><li><a href="/"> parent directory</a></li> <li><a href="random"> random/</a></li> <li><a href="random_2/"> random_1/</a></li> <li><a href="radnfdom"> random/</a></li> <li><a href="rasrdndddom_1/"> random_1/</a></li> <li><a href="random_43"> random/</a></li> <li><a href="test/"> random_1/</a></li> </ul> </body></html>

in other words can strip of html tags improve speed, search plain text in html text document.

you can watch script in action here @ tdsoft.se

<html> <head> <script type="text/javascript"> var txt; var buildname = ""; var xmlhttp; function lcs(a, b) { var asub = a.substr(0, a.length-1); var bsub = b.substr(0, b.length-1); if (a.length == 0 || b.length == 0) { homecoming ""; } else if (a.charat(a.length-1) == b.charat(b.length-1)) { homecoming lcs(asub, bsub) + a.charat(a.length-1); } else { var x = lcs(a, bsub); var y = lcs(asub, b); homecoming (x.length > y.length) ? x : y; } } function loadxmldoc(url,cfunc) { if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=cfunc; xmlhttp.open("get",url,true); xmlhttp.send(); } function myfunction() { loadxmldoc("http://tdsoft.se/testni.html",handlexml); } var checkstate = function(xmlhttp, callback) { try{ if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { callback(); } else { // check 1 time again 1 sec later settimeout(checkstate, 1000); } } catch(err){ settimeout(checkstate, 1000); } }; function handlexml() { checkstate(xmlhttp, function() { txt=xmlhttp.responsetext; buildname = "random"; txt = txt.replace(/<&#91;^>&#93;*>/g, ""); var myvar = ""; myvar = lcs(txt, "random"); document.write(myvar); }); } </script> </head> <body onload="myfunction()"> </body> </html>

seems want take different approach @ this.

i'm not sure point of trying seems want:

you request document parse links in document , store them in object keyed ids values beingness text change lookup function go after link list

here's code illustration (using jquery simplicity):

//untested! var links = {}; function successfunction(data) { var atags = data.find('a'); atags.each(function() { var $this = $(this); links[$this.attr('href')] = $this.text(); }); } function lookup(id) { homecoming links[id] || ''; } $.ajax({ url: 'requestpage.htm', success: successfunction });

edit:

if want non-jquery can replace next things:

$.ajax xmlhttprequest method data.find('a') getelementsbytagname .each(function(){...}) var = atags.length; while(i--) { links[atags[i].href] = atags[i].innerhtml; }

javascript html

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -