5

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)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    I'm not sure if thats possible. JS has scope will page loaded in its window and search feature is a part of browser. You can though try to have your own search control and on `enter`, search in loaded document and mimic the behaviour. Something similar to slack, but instead of loading another user's chat, search in current page. – Rajesh Dec 23 '16 at 08:20
  • Probably [that answer](http://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programatically) will help you – Aminur Rashid Dec 23 '16 at 08:35
  • @AminurRashid I do not see how this might help. Can you explain? – Uwe Keim Dec 23 '16 at 08:52

1 Answers1

6

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

setTimeout(function() {
  find('text');
}, 1000);
<p>I'm the text</p>  
Ori Drori
  • 183,571
  • 29
  • 224
  • 209