Search code examples
javascripthtmlsearchbrowser

How can I programmatically trigger the search of my browser?


What I like to do is to programmatically trigger the on-page search functionality of my browser from JavaScript.

I.e. when a page is loaded and the the user presses Ctrl+F, a search field is opened and the user can enter a search term. This usually results, depending on the used browser, in all occurances being highlighted.

I want to do the same on my page:

  1. Call some JavaScript API of the browser to trigger the search field.
  2. Call some JavaScript API of the browser to enter values into the search field.
  3. Actually execute the search.

While I think it is not possible, due to possible security-risks, I still want to be sure.

Therefore my question:

Is it possible to programmatically trigger the on-page search function of a web browser?

(If there is no general API, maybe there are browser-specific ways of doing it)


Solution

  • There is the non standard window#find, currently supported by Chrome, FF, and Safari. However, future support is not guaranteed:

    setTimeout(function() {
      find('text');
    }, 1000);
    <p>I'm the text</p>