Search code examples
javascriptfirefoxkeypress

Prevent automatic search in Firefox using JavaScript


I would like to use JavaScript to disable Firefox's automatic search function (a search box appears on a keypress outside an input area, even without explicitly calling Ctrl+F).

I can use

$(window).keypress(function(e)
{
    e.preventDefault()
    // other code
}

But this disables the default action for all keypresses, like Ctrl+T to open a new tab.

Is there a way to disable the search function selectively?


Solution

  • You could just prevent the Firefox' search box with the key presses you want to have in your webpage. All others would still open the search box - which might be even a nice feature.

    E.g. if you are expecting input in the number keys 1 - 4 you could use:

    $(window).keypress(function(e) {
      var keycode = (e.keyCode ? e.keyCode : e.which);
      // Prevent default for keys 1, 2, 3, 4
      if (keycode === 49 || keycode === 50 || keycode === 51 || keycode === 52) {
        e.preventDefault();
      }
    });