This javascript adds a button called "Find on This Page..."
to your HTML document.
When the button is pressed a pop-up window (DIV) comes on
the HTML document and asks for the text to search for and then
the user can press "Next" or "Prev" to search for all of the
occurrences of the search term.
New as of 5/30/2016!! I just finished writing a
Fixed Position Edition of the Cool Javascript Find on this page script. It
displays a button that stays locked to the bottom right corner of the screen.
The button will have a magnifying glass on it or the word "Find" for older
browsers.
New as of 7/16/2014!! This javascript
has had an almost complete rewrite of the searching and highlighting
functions. The script no longer uses WebKit's window.find() function
or Internet Explorer's createTextRange() and findText() functions.
Instead the script now traverses all elements in the document or in the variable
find_root_node specified such as a div.
Example: var find_root_node = 'mydiv'; // id of div to search
Now all occurrences of the search term in the document
are highlighted and the user can jump to each highlight with the "Next" and "Prev"
buttons or search for a new search term in which case the previous search phrases
are unhighlighted first.
If you need the previous "Find on this page..." script then
click here. The previous
version worked in Internet Explorer, Firefox, Chrome, Netscape, Safari and Android but
it did not work in iPhone or iPad iOS or in Opera. The old version of
this script also did not highlight every occurrence of the search term in the
document. Instead it would only highlight the next match when "Next" was pressed.
Features
- Find on this page function for IE, Chrome, Opera, Android,
iPhone, Netscape, Firefox and Safari.
- Can search forwards and backwards on the page.
- Jumps to and highlights the found search term.
- The pop-up is a DIV instead of an actual window so
that unintelligent pop-up blockers will not block it.
- The pop-up DIV always stays in view even if the page
scrolls.
- Added mouse functions to be able to move the DIV around
the screen with the mouse.
- Added keyboard functions so the user can press ENTER to
search for the next occurrence of the word or term.
- Now includes a radio button to choose site search or page search.
dlc_b
Download
Downloaded 0 times.
Please make a donation to reveal the download link.
Then in your HTML document paste the following code where you
want the "Find on this Page..." button to appear:
All matches are currently highlighted in yellow and the current selection
is highlighted in orange. You can change the highlighting by creating
classes called .highlight and .find_selected as shown in the box below:
Notes:
Update 7/15/2024
Version 5.4j - Now automatically opens <details> tags if the current find is found within it.
Update 8/31/2022
Version 5.4i - Added new option called find_start_at_scroll. If set to true or 1 then it will
start finding the search text at the current scroll position height instead of at the beginning of the
document. Also the selection of the last highlighted find is now unselected when starting a new find search.
Also the user can now scroll the find window even if it is position fixed with find_window_fixed = 1;
Update 8/23/2021
Version 5.4h - Improvements to how the find window is displayed including: Closing X is bigger.
More elements on one line. Width of find window changed from 245 to 255.
Update 8/17/2021
Version 5.4g - Mobile device improvements: Added font-size:16px to search text box to try and
prevent smaller phones from zooming in on text box focus. It works sometimes but still zooms in
a little bit on some smaller iPhones. Added code to prevent mobile devices from scrolling the page
when the user is trying to move the find window with their finger. Also added title attributes of
"Find Previous" and "Find Next" to the buttons for accessibility.
Update 8/16/2021
Version 5.4f - Fixed a bug where Chrome for Android was not scrolling to first find because
the keyboard was apparently closing too slowly. So added a delay before scrolling to the found text.
Also added option to keep find window fixed at top of screen instead of letting the user move it with
the mouse and having it bounce back on the screen when the user scrolls. Also now the search can start
by using the previous button and not just the next button or the enter key. Also changed the buttons
from Find Prev and Find Next to < and > .
Updated 12/15/2020
Version 5.4e - Increased z-index and added branding. Fixed styles for closing X
Update 8/5/2019
Version 5.4d - Changed scrollIntoView() code to be able to scroll to the next found match
better if the scrolling element in your document is not the body element. Also will use smooth
scrolling and put found match in middle of screen if your browser supports it and the found
match is not already on the screen. (isOnScreen() and scrollToPosition())
Update 8/5/2019
Version 5.4c - Bug Fix in Firefox 68. Firefox was sometimes having the error:
"InvalidAccessError: A parameter or an operation is not supported by the underlying object"
on line 703 where the script is checking for highlight rules in the styleSheets.
This is an old Firefox bug that they fixed at one time but it looks like they have it again
in Version 68. It happens when Firefox sometimes starts processing JavaScript before all
the styleSheets are loaded. If there is a styleSheet in the HTML code after the JavaScript
file being processed then it breaks the operation of the code. I believe the bug is fixed
by using a try and catch statement so that the code does not break.
Update 7/2/2019
Verison 5.4b - Bug fix - Changed searching in input elements to only work with type
textarea|text|number|search|email|url|tel so that imput types like "image" will still
display the image and not be erased with a pre tag.
Update 7/20/2018
Version 5.4 - Now a simple site search can be enabled for the Javascript Find on this page window.
Edit find5.js and change: var enable_site_search = 1; to enable it. It uses Google's
site search and your domain name to open a new window with a url like this example:
https://www.google.com/search?q=site%3Awww.seabreezecomputers.com+superhero
Update 7/20/2018
Version 5.3f - No longer using find_window_height because it really isn't needed and
was messing up the display of the find window adding a scrollbar at times. Also Bug Fix:
In Firefox and IE 10 and above input elements and textareas were not copying the cssText
of the original element when converting it to a pre because of a bug in these browsers not
supporting cssText properly. This has been fixed.
Update 3/24/2018
Version 5.3e - find5.js now finds text in input elements by copying the content
of the input element into a pre element and displaying the pre element instead of the input elment.
Then if the user clicks on the pre element it displays the input element again.
Just like the script is already doing with textarea elements.
Update 2/22/2018
Version 5.3d - Bug Fix: Changed line 458 to if (String(fobj.className).match(/dragme/i)) (adding the String() to the line)
to prevent an error message in Developer tools console: "fobj.className.match is not a function".
Update 7/15/2016
Version 5.3c - Bug Fix (Thanks to dev for finding it): Could not search for * or ( without getting
error in developer console Uncaught SyntaxError: Invalid regular expression. Fixed by commenting
out line 71. //var re = new RegExp(word, "i"); // regular expression of the search term. This
was left over from when the script was searching for the term using regular expressions.
Update 2/11/2016
Version 5.3b - Bug Fix (Thanks to George for finding it): Could not search for $ or ^ without freezing
the browser in Chrome, Firefox and Internet Explorer. This is because find5.js was using regular
expression search(). So special reguar expression variables could not be searched for. This has
been fixed by using toLowerCase().indexOf() now instead of regular expression search().
Hopefully no one needs to use regular expressions in the search, but if they do then they could
uncomment line 83 and comment line 84.
Update 5/15/2015
Version 5.3 - Bug Fix (Thanks to Jay for finding it): Previously if you clicked "Find Next" the button
would remain focused so that if you pressed the Enter key there would be a double search
and one of the finds would be skipped. Fixed by adding this.blur(); to line 676.
Also iOS was not selecting the text in the find window if the user reopened the window.
Apparently textbox.select() does not work in iOS. But added textbox.setSelectionRange(0, 9999);
to line 341 and now iOS Safari selects the text. Also in this version changed the find window
width to 245px because in iOS the number of finds was being cut off.
Update 3/10/2015
Version 5.2 - Bug fix in IE: Previously if you searched for part of a word such as "Java"
in "Javascript" then IE would find the matches. But if you then searched for the whole
word such as "Javascript" then IE would not find the matches. This is because IE does not
use the normalize() method on text nodes the way other browsers do. Other browsers join
adjacent text nodes and remove empty ones. Apparently IE does not remove empty text nodes
with normalize(). So I added a normalize(node) function to find5.js to fix this bug in IE.
Update 12/2/2014
Version 5.1a - Minor change: Changed default width of find window to 225px
instead of 235px so that it displayed better on iPhone screens. It was
difficult to press the closing x when it was wider.
Update 10/17/2014
Version 5.1 - A number of features and bug fixes:
- When closing the find window the previous finds are unhighlighted
and the current find is selected.
- Changed the formatting to text window to be more appealing. It can
also be customized.
- Added placeholder="Enter text to find" in text input field.
- After pressing Search the text input box is blurred so that the
keyboard closes on iOS and Android devices.
- Escape key closes find window.
- On reopening the find window the last search text is selected so user can
search again or start typing a new search.
- The find window no longer drags when clicking in the text input field
so that text can be selected with the mouse
- Removed zIndex in mouseDown function because it may have caused "Invalid
Argument" in IE 8.
Update 10/5/2014
Version 5.0d - Bug Fix - Versions of IE versions 9 and earlier were having
an error with the mouse functions. Line 418 "target is null or not an object".
Fixed by putting if (!event) event = window.event; on lines 417 and 486.
Update 8/25/2014
Version 5.0c - Bug Fix - I accidentally introduced a bug in version 5.0b that
caused the close button and "prev" and "next" buttons to not work on iPhone
or Android devices. Fixed the bug by changing line 475 to return true;
Update 7/30/2014
Version 5.0b - Bug Fix - If the doctype was set to an xhtml doctype
then the find window could not be dragged with the mouse. This is
because coordinates for the screen have to be followed by "px" in
xhtml. So added + "px" to the end of lines 514 and 515.
Update 7/18/2014
Version 5.0a - Bug Fix - At first I was changing the find_root_node to search
with var find_root_node = document.getElementById('mydiv'); However,
this did not work because the div did not exist until the page was loaded.
Instead now the user specifies just the id of the div. For example:
var find_root_node = 'mydiv'; and the findit() function will create
the reference to the div before it calls the highlight() function.
Update 7/16/2014
Version 5.0 - Complete rewrite of the searching and highlighting functions.
Now all text nodes in the document or the specified div are searched through.
All matches are highlighted. The script checks for elements with visibility
set to hidden or display set to none and does not search for the term in
those elements. HTML textarea's are highlighted with a trick because I can't
find a way to highlight multiple search terms in a textarea with a span. So
the script copies the value of the textarea to a pre tag and highlights
the matches in the pre tag while turning the textarea invisible. If the pre
tag is clicked on then the textarea becomes visible again.
Update 6/16/2014
Version 2.3e - Bug Fix - The below bug fix for IE 11 was not working. Since
IE 11 no longer supports detecting IE with document.all the code has been
changed by detecting if the browser supports window.find starting on
line 51. So far no versions of IE support window.find, but Chrome and Firefox do.
Update 11/4/2013
Version 2.3d - Bug Fix - Internet Explorer 11 was not working with find2.js.
Updated lines around line 43 to fix detection of IE 11 because IE 11 removed
support for detection of document.all.
Thanks to Warren Yates for the fix.
Update 9/14/2013
Version 2.3c - Bug Fix - An HTML 5 doctype of <!DOCTYPE html> was causing
current_bottom to be set incorrectly in the move_window() function. So the
window was constantly jumping up and down. Fixed by replacing current_bottom and
current_right declarations with var current_bottom = (window.innerHeight ||
document.documentElement.clientHeight || document.body.clientHeight) + current_top;
Hopefully this does not cause a problem with certain browsers and/or doctypes.
Update 8/22/2013
Version 2.3b - Bug Fix - The "next" button was displaying "Found" and "Not Found",
but the "Previous" button was displaying "true". Changed "Previous" to display
"Found" and "Not Found".
Update 7/23/2013
Version 2.3 - Bug Fix - Update for Internet Explorer. If the webpage has a menu
system with display:none elements and IE's findText function finds text in this
menu system then when we try to highlight the text with txt.select(); IE throws an error:
Could not complete the operation due to error 800a025e. Fixed the error by adding
try and catch around line 103.
Update 10/16/2012
Version 2.2 - Bug Fix - Update for Firefox. No longer tries to get a selection
of the text if the search term was not found. This might be able to avoid a bug
in Firefox with getRangeAt(0);
Update 3/29/11
version 2.1 - Bug fix - In firefox the window.find function was not searching
at the beginning of the document, but where the "find on this page" button was
initially called with the find2.js file. This has been fixed by changing line
477 of the script to use document.body.insertBefore(findwindow, document.body.firstChild);
instead of document.body.appendChild(findwindow);
Update 1/28/2011
version 2.0 - Major revision. Now the findwindow DIV is created dynamically
in javascript and attached to the body of the DOM. This eliminates a problem
of the find window not being able to determine its location if the find button
and div is nested in many other divs.
Update 8/9/2010
Fixed a bug where the text search would find the text in
the findwindow div. Fixed by making the div hidden while searching
and then making it visible afterward. This also fixed a bug in
Firefox: uncaught exception: [Exception... "Component returned
failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsISelection.getRangeAt]"
which happens when the range is selected with getRangeAt on
an <input type='text' element or a <textarea element. So this
exception may still appear in Firefox's error console if a search is made
on your page and the text is found in one of those html elements. (v. 1.3c)
Update 9/7/2009
Now there is a version of the find script that searches
back at the beginning of the document if the search term
is not found at the end. This loop searching version can
be downloaded here:
dlc_b
Download
Downloaded 0 times.
Please make a donation to reveal the download link.
Update 6/5/2009
This script seems to work in Safari now. But the find button
does not even show up in Opera browser.
There was a bug in Firefox where if the find box was
blank and the user pressed "Next" then the default Firefox
find box would come up. I believe I have solved this problem
by adding a check to see if the find box is empty. I
added this line to findit() and findprev() functions
as follows:
if (string != "")
test.innerHTML = window.find(string, false, false);
Update 5/30/2008
version 1.3b - Changed all html code to lowercase to work with a more
strict doctype and xhtml. Also changed <br> tags to
<br /> .
Update: 9/18/2007
This script now uses the same DIV in Firefox and Netscape. I
figured out how to use the
window.find(string, caseSensitive, searchBackwards) function
in Firefox and Netscape. It works great in Netscape and we
just have to use these two functions to search forwards and
backwards:
window.find(string, false, false); // Search Forwards
window.find(string, false, true); // Search Backwards
You can still choose to use the built-in search window
with this script by changing the variable mozilla_opt to 0.
However certain versions of Netscape don't seem to find
the search terms when you call the built-in search window
with window.find().
For Firefox I had to add a few functions because Firefox
loses focus on the selection when you press the Next or Prev
buttons. So it could only find the first occurrence of the
search term over and over. So for firefox I had to add the
following functions to record the highlighted selection:
sel = window.getSelection(); // get selection
range = sel.getRangeAt(0); // get object
Then I had to use the following functions to highlight
the recorded search term again after a button was pressed:
sel = window.getSelection(); // get selection
if(sel.rangeCount > 0) sel.removeAllRanges(); // remove all ranges
sel.addRange(range); // add last highlighted range
Last updated on July 16, 2024
Created on September 10, 2007 |