42

I have created a website and have added a button and lot of text. What I want is to use the browser search (Ctrl+F), when I press the button which I added in website.

How can I achieve this?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Ashok Gupta
  • 2,247
  • 5
  • 28
  • 34

3 Answers3

22

It kind of works for FF. See this page

 <a href="#" onclick="find();">Find in This Page...</a>
Junaid
  • 1,708
  • 16
  • 25
  • 4
    You can also pass a string to it, `find('foobar')` will highlight the first occurrence of "foobar" in the document. Calling `find()` again will highlight the next occurrence. – Fabian Nov 10 '11 at 15:18
  • 1
    http://www.w3resource.com/javascript/client-object-property-method/window-find.php say list of supported browsers: Internet Explorer 7, Firefox 3.6, Google Chrome 7, Safari 5.0.1, Opera 10. – Enyby Aug 28 '14 at 20:18
  • From my testing window.find() is supported in Chrome 37, and FF31, but not IE11. I'd be careful in terms of future support though: https://bugzilla.mozilla.org/show_bug.cgi?id=672395 – Mark Sep 04 '14 at 04:49
  • 2
    @Fabian - find() highlights the text in a webpage, but it does not scroll down to show the highlighted text (unlike the browser's search capability which scrolls down the page and show the highlighted text). – Mohan P Apr 19 '15 at 08:15
19

update: from mark's comment:

From my testing window.find() is supported in Chrome 37, and FF31, but not IE11

update:

for more information on this window.find go here

Vignesh
  • 1,045
  • 2
  • 17
  • 34
  • 1
    From my testing window.find() is supported in Chrome 37, and FF31, but not IE11. – Mark Sep 04 '14 at 04:48
  • thanks @Mark, I updated my answer by using your answer – Vignesh Sep 13 '14 at 13:08
  • 2
    hello, this worked for me too. I just additionally want that whenever i use find() again, it should search from the top on the page (and not after previously found literal). Can you tell how to reset the find() function? or lead me to the documentation of it? – Abdul Wasae Jul 07 '15 at 11:38
  • 1
    nevermind. got it :) https://developer.mozilla.org/en-US/docs/Web/API/Window/find – Abdul Wasae Jul 07 '15 at 11:44
10

No. In general, you cannot invoke browser controls from inside the webpage - security, sandboxing, and all that.

You need to make some sort of in-page search - if you're using a CMS, most of them have a simple search feature built-in.

If you want to do this in JavaScript, there are various scripts that emulate this in-page; randomly selected from my query "javascript find in page" is this one: http://www.seabreezecomputers.com/tips/find.htm

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222