find method (window)
Searches for a specified text in the document and highlights the matches.
The start position of the search depends on the current selection.
When forward search is set, the start position of the search is the end position of the selection.
When backward search is set, the start position of the search is the position before the end position of the selection.
If no content is selected, the start position of the forward search is the first position of the document, the start position of the backward search is the last position of the document.
Use the window.getSelection method to retrieve a selectionRange object.
With this object you can get or modify the selected content in the document.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Optional. String that specifies the text to find. If this parameter is not set or its value is an empty string, the Find dialog box appears in Firefox and nothing happens in Google Chrome and Safari. | |||||||||||||||||||||||
Optional. Boolean value that specifies the case sensitivity.
One of the following values:
|
|||||||||||||||||||||||
Optional. Boolean that specifies the search direction.
One of the following values:
|
|||||||||||||||||||||||
Optional. Boolean that indicates whether to search in wrapped text, or not.
One of the following values:
|
|||||||||||||||||||||||
Optional. Boolean that indicates whether to match whole words only, or not.
One of the following values:
|
|||||||||||||||||||||||
Optional. Boolean that indicates whether to also search in frames, or not.
One of the following values:
|
|||||||||||||||||||||||
Optional. Boolean that indicates whether to show the find dialog of the browser.
One of the following values:
|
Return value:
Boolean. One of the following values:
No match was found. | |
A match was found. |
Example HTML code 1:
This example shows how to find text within a page:
|
||||
<head> <script type="text/javascript"> function FindNext () { var str = document.getElementById ("findInput").value; if (str == "") { alert ("Please enter some text to search!"); return; } if (window.find) { // Firefox, Google Chrome, Safari var found = window.find (str); if (!found) { alert ("The following text was not found:\n" + str); } } else { alert ("Your browser does not support this example!"); } } </script> </head> <body> <div>LaLa, Lala, laLa , lala, lalala, tralala, some other text</div> <br /> <input type="text" id="findInput" value="lala" size="20" /> <button onclick="FindNext ();">Find Next</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates a cross-browser solution for the previous example:
|
||||
<head> <script type="text/javascript"> function FindNext () { var str = document.getElementById ("findField").value; if (str == "") { alert ("Please enter some text to search!"); return; } var supported = false; var found = false; if (window.find) { // Firefox, Google Chrome, Safari supported = true; // if some content is selected, the start position of the search // will be the end position of the selection found = window.find (str); } else { if (document.selection && document.selection.createRange) { // Internet Explorer, Opera before version 10.5 var textRange = document.selection.createRange (); if (textRange.findText) { // Internet Explorer supported = true; // if some content is selected, the start position of the search // will be the position after the start position of the selection if (textRange.text.length > 0) { textRange.collapse (true); textRange.move ("character", 1); } found = textRange.findText (str); if (found) { textRange.select (); } } } } if (supported) { if (!found) { alert ("The following text was not found:\n" + str); } } else { alert ("Your browser does not support this example!"); } } </script> </head> <body> <div>LaLa, Lala, laLa , lala, lalala, tralala, some other text</div> <br /> <input type="text" id="findField" value="lala" size="20" /> <button onclick="FindNext ();">Find!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
Related pages:
External links:
User Contributed Comments