javascript - Search for string in multiple html files with jquery -
i'll seek explain problem few words. have html various iframes. in 1 iframe there table of contents (toc) , in other content of corresponding element highlighted in toc. since there various tocs, might happen clicking on link, we'll taken topic belongs toc, , hence want toc frame reloaded proper toc. so, since each topic has unique id within toc, perform search of id of topic loaded in main frame accross tocs , when find wanted toc, load in toc frame.
the code i've written far seguent:
/*function called on load of each topic - gets topic unique id parameter*/ function highlight(id) { /*the names of html files containing different tocs*/ var tocs = ["toc.htm", "toc_documentazione.htm", "toc_flussiesteri.htm", "toc_garante.htm", "toc_legittimita.htm", "toc_normativa.htm", "toc_settori.htm", "toc_sicurezza.htm", "toc_sistemadp.htm", "toc_vistaarticolato.htm"] var = 0; /*search within different tocs until find correspondence or there no more tocs*/ while (!changetoc(tocs[i], "a" + id) && < tocs.length) { = + 1; } /*this line wrong thought load found toc in appropriate frame*/ $(content).load(tocs[i - 1] + " #content"); } /*function using ajax search id html file passed parameter (newtoc) returning search outcome*/ function changetoc(newtoc, id) { var found = false; $.get(newtoc, "html").done( function(temp_toc) { /*if html contains id search homecoming true*/ if (temp_toc.indexof(id) != -1) found = true; }); /*else homecoming false*/ homecoming found; }
the problem have while cycle utilize search through various toc files. did debugging and, regardless fact toc containing id i'm searching @ first position, while extecutes 10 cycles , @ lastly tells me has found matching toc, indeed first 1 in list.
hope i've been able create myself clear. help
i managed done using ajax phone call syncronus set true. i'm not posting here code cause confusing, below changed compared code written above , works fine. maybe it's not optimal in terms of performance don't have concern, i'm happy :) hope can help others.
/*function called on load of each topic - gets topic unique id parameter*/ function highlight(id) { var tmpval = sessionstorage.getitem('key'); //we check if there element in toc highlighted in bold, if remove highlight if(tmpval) { var tmpele=parent.window.frames[0].document.getelementbyid('a'+tmpval); if (tmpele) { tmpele.classname=''; } } //loop through tocs find 1 containing selected topic var tocs = ["toc.htm","toc_documentazione.htm","toc_flussiesteri.htm","toc_garante.htm","toc_legittimita.htm","toc_normativa.htm","toc_settori.htm","toc_sicurezza.htm","toc_sistemadp.htm","toc_vistaarticolato.htm"]; var i=0; while (!changetoc(tocs[i],"a"+id)&&i<tocs.length){ i=i+1; } //get loaded toc var currenttoc=$("#toc_iframe",parent.document).attr("src"); var indexcurrenttoc=tocs.indexof(currenttoc); //we check if matching toc current one, if don't alter if(!changetoc(tocs[indexcurrenttoc],"a"+id)){ $("#toc_iframe",parent.document).attr("src",tocs[i]); } var myelt=parent.window.frames[0].document.getelementbyid('a'+id); //highlight current element in toc myelt.focus(); myelt.classname+=' active'; scrollto(myelt.offsetleft-48, myelt.offsettop-(parent.document.body.clientheight/3)); sessionstorage.setitem("key", id); } //searches element given id toc file newtoc function changetoc(newtoc,id){ var found = false; $.ajax({ url: newtoc, async: false, context: document.body }).done(function(temp_toc) { if(temp_toc.indexof(id)!=-1){ found = true; } }); homecoming found;
}
javascript jquery html
No comments:
Post a Comment