Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match all occurrences with window.find()

Tags:

javascript

For example if I have a web page HTML like below

<body>
        Hello Techies, <br>
        Techies here.
</body>  

If I search for "Techies" using

 var sel = window.getSelection(); 
 sel.collapse(document.body, 0); 
 document.body.offsetHeight;
 if (window.find("Techies", true)) { 
   document.execCommand("hiliteColor", false, "YellowGreen"); 
   sel.collapseToEnd(); 
 }

It is Highlighting only the first occurred of the "Techies". But when I search with Ctrl+F the first occurrence will be highlighted in Dark and next occurrences will be highlighted in light color mode. How can I achieve the same with the above code.

like image 611
Exception Avatar asked Feb 25 '12 19:02

Exception


1 Answers

Try using a while loop:

if (window.find("Techies", true)) { 
   document.execCommand("hiliteColor", false, "FirstColor");
   while (window.find("Techies", true)) {
      document.execCommand("hiliteColor", false, "SecondColor");
   }
   ...
}
like image 166
raina77ow Avatar answered Oct 16 '22 23:10

raina77ow