ios5 - Jump through search results in UIWebView with Javascript on the Button click -
i have set search in uiwebview javascript works great, want able jump next found word in search results. have succeeded in geting view scroll first instance using code:
function uiwebview_highlightalloccurencesofstringforelement(element,keyword) { //declared var height if (element) { if (element.nodetype == 3) { // text node while (true) { //if (counter < 1) { var value = element.nodevalue; // search keyword in text node var idx = value.tolowercase().indexof(keyword); if (idx < 0) break; // not found, abort //(value.split); //we create span element every parts of matched keywords var span = document.createelement("span"); var text = document.createtextnode(value.substr(idx,keyword.length)); span.appendchild(text); span.setattribute("id",keyword); span.setattribute("class","uiwebviewhighlight"); span.style.backgroundcolor="black"; span.style.color="white"; uiwebview_searchresultcount++; // update counter text = document.createtextnode(value.substr(idx+keyword.length)); element.deletedata(idx, value.length - idx); var next = element.nextsibling; element.parentnode.insertbefore(span, next); element.parentnode.insertbefore(text, next); element = text; if(desiredheight == 0) { var offset = {}; offset.x = 0; offset.y =0; getoffset(document.getelementbyid(keyword),offset); desiredheight = offset.y-150; alert(desiredheight); window.scrollto(0,desiredheight); } } } else if (element.nodetype == 1) { // element node if (element.style.display != "none" && element.nodename.tolowercase() != 'select') { (var i=element.childnodes.length-1; i>=0; i--) { uiwebview_highlightalloccurencesofstringforelement(element.childnodes[i],keyword); } } } } } function getoffset (object, offset) { if (!object) return; offset.x += object.offsetleft; offset.y += object.offsettop; getoffset (object.offsetparent, offset); }
now want jump next search string on next button click. how can it? please suggest me.
it's function necessary functionality:
// we're using global variable store number of occurrences var myapp_searchresultcount = 0; var mycurrentcount = 0; // helper function, recursively searches in elements , child nodes function myapp_highlightalloccurencesofstringforelement(element,keyword) { if (element) { if (element.nodetype == 3) { // text node while (true) { var value = element.nodevalue; // search keyword in text node var idx = value.tolowercase().indexof(keyword); if (idx < 0) break; // not found, abort var span = document.createelement("span"); var text = document.createtextnode(value.substr(idx,keyword.length)); span.appendchild(text); var str1 = "myapphighlight"+myapp_searchresultcount; span.setattribute("class", "myapphighlight"); span.setattribute("id", str1); span.style.backgroundcolor="yellow"; span.style.color="black"; text = document.createtextnode(value.substr(idx+keyword.length)); element.deletedata(idx, value.length - idx); var next = element.nextsibling; element.parentnode.insertbefore(span, next); element.parentnode.insertbefore(text, next); element = text; myapp_searchresultcount++; // update counter } } else if (element.nodetype == 1) { // element node if (element.style.display != "none" && element.nodename.tolowercase() != 'select') { (var i=element.childnodes.length-1; i>=0; i--) { myapp_highlightalloccurencesofstringforelement(element.childnodes[i],keyword); } } } } } // main entry point start search function myapp_highlightalloccurencesofstring(keyword) { myapp_removeallhighlights(); myapp_highlightalloccurencesofstringforelement(document.body, keyword.tolowercase()); mycurrentcount = myapp_searchresultcount; movetonext(); } // helper function, recursively removes highlights in elements , childs function myapp_removeallhighlightsforelement(element) { if (element) { if (element.nodetype == 1) { if (element.classname.indexof("myapphighlight") > -1) { var text = element.removechild(element.firstchild); element.parentnode.insertbefore(text,element); element.parentnode.removechild(element); return true; } else { var normalize = false; (var i=element.childnodes.length-1; i>=0; i--) { if (myapp_removeallhighlightsforelement(element.childnodes[i])) { normalize = true; } } if (normalize) { element.normalize(); } } } } return false; } function movetoprev() { if (mycurrentcount < myapp_searchresultcount-1) { mycurrentcount++; document.getelementbyid("myapphighlight"+mycurentcount).scrollintoview(); } else{ mycurrentcount = myapp_searchresultcount; window.scrollto(0,0); } } function movetonext() { if (mycurrentcount > 0) { mycurrentcount--; document.getelementbyid("myapphighlight"+mycurentcount).scrollintoview(); } else{ mycurrentcount = myapp_searchresultcount; window.scrollto(0,0); } } // main entry point remove highlights function myapp_removeallhighlights() { myapp_searchresultcount = 0; mycurrentcount = 0; myapp_removeallhighlightsforelement(document.body); }
Comments
Post a Comment