Last active
December 22, 2025 02:00
-
-
Save yonixw/e884162062046eceb0890ff27477a78d to your computer and use it in GitHub Desktop.
HIGHLIGHT JS CODE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://stackoverflow.com/questions/16251505/how-to-highlight-all-text-occurrences-in-a-html-page-with-javascript | |
| /* | |
| * Search for text in the window ignoring tags | |
| * | |
| * Parameters: | |
| * text: a string to search for | |
| * backgroundColor: | |
| * "yellow" for example, when you would like to highlight the words | |
| * "transparent", when you would like to clear the highlights | |
| * */ | |
| function doSearch(text, backgroundColor) { | |
| if (window.find && window.getSelection) { | |
| document.designMode = "on"; | |
| var sel = window.getSelection(); | |
| sel.collapse(document.body, 0); | |
| while (window.find(text)) { | |
| document.execCommand("HiliteColor", false, backgroundColor); | |
| sel.collapseToEnd(); | |
| } | |
| document.designMode = "off"; | |
| } | |
| } | |
| doSearch("long first","green") | |
| doSearch("second","pink") | |
| doSearch("first","yellow") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment