faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

66 of 74 people (89%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I find text in the page?
How can I do window.find('str') with IE?

Nov 28th, 2000 18:31
Martin Honnen,


NN4+ provides the
  window.find
method which when called without argument opens the find dialog and 
when called with a string argument searches for the string in the page 
and selects it when found. It returns true when a search is successful 
and false otherwise.
Examples:
  window.find(); // opens the find dialog
  var found = window.find('Kibology'); // searches case insensitive
  var found = window.find('Kibology', true, false); 
  // searches case sensitive forward
  var found = window.find('Kibology', false, true);
  // searches case insensitive backwards
IE4+ doesn't provide this method but has a range object which allows 
for searching and selecting text. You create a range with
  var range = document.body.createTextRange();
find a text with
  var found = range.findText('Kibology');
select it with
  range.select()
and scroll it into view
  range.scrollIntoView()
Note that the IE method finds text in textareas and input elements too 
while NN doesn't do that.