2

I am trying to bring up the browser search from a link click.

I found there are some functions like window.find(), but they don't work with all browsers. So the best way to achieve this is, simulate ctrl + f.

I have this following code, but this is not working, I don't know why.

function bringUpSearch() {

  var keyboardEvent = document.createEvent("KeyboardEvent");
  var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";


  keyboardEvent[initMethod](
    "keydown", // event type : keydown, keyup, keypress
    true, // bubbles
    true, // cancelable
    window, // viewArg: should be window
    true, // ctrlKeyArg
    false, // altKeyArg
    false, // shiftKeyArg
    false, // metaKeyArg
    102, // keyCodeArg : unsigned long the virtual key code, else 0
    0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
  );
  document.dispatchEvent(keyboardEvent);
}
<a href="#" onclick="bringUpSearch()">Bring up browser search</a>

3

1 Answer 1

2

You can't do that in Chrome, since there is no API for that. See the list of Chrome Extensions Documentation

In those ancient manuscripts people mention Find Dialog: window.find(), that you were able to open the dialog, by passing true as the last argument, but it's not supported for ages.

What you can is to make a custom search textfield, grab the user input from it and run window.find

Sign up to request clarification or add additional context in comments.

2 Comments

This script does not work in any browser, so it's not Chrome only, I want to know what's the problem with this script
@SoumitriPattnaik I have updated my answer, it seems like back in times there were an option to open a dialog. Not anymore!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.