How to search text and highlight the text that matches in a webpage.
Use the following function and pass the string to be searched in the function as parameter.
var n = 0; function Findstring(searchstring) { if (document.all) { var txt = window.document.body.createTextRange();//create a text range i.e all text in webpage for (i = 0; i <= n && (found = txt.findText(searchstring)) != false; i++) { txt.moveStart("character", 1);//move to next txt.moveEnd("textedit");//move to end } if (found) { txt.moveStart("character", -1); txt.findText(searchstring); txt.select();//hightlight the text txt.scrollIntoView(); n++; } else { if (n > 0) { n = 0; Findstring(searchstring); } else { alert("Sorry, we couldn't find.Try again"); } } } return false; }
It will highlight the string if any matched case will be found .It works in IE4+.
Comments
Post a Comment