最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to bypass Quick Search Firefox feature and capture forward slash keypress - Stack Overflow

programmeradmin0浏览0评论

I'm capturing the key press value of '191' for the forward slash (/) for a feature on my site. Works fine on every browser except Firefox due to its Quick Search feature. The '191' still registers and the action is executed (focus on an input field, popup help text), but the focus goes to the Quick Search.

I read in another StackOverflow question saying that Firefox captures the forward slash as character code '0', but that didn't do anything.

Is there a way I can ignore the Firefox Quick Search and get control of the forward slash back? Using JavaScript and jQuery.

I'm capturing the key press value of '191' for the forward slash (/) for a feature on my site. Works fine on every browser except Firefox due to its Quick Search feature. The '191' still registers and the action is executed (focus on an input field, popup help text), but the focus goes to the Quick Search.

I read in another StackOverflow question saying that Firefox captures the forward slash as character code '0', but that didn't do anything.

Is there a way I can ignore the Firefox Quick Search and get control of the forward slash back? Using JavaScript and jQuery.

Share Improve this question asked Oct 17, 2011 at 18:29 tridiumtridium 3211 gold badge5 silver badges12 bronze badges 3
  • 3 Ask first whether you should do this. As a general rule, I would think twice (or more) before doing anything that alters the expected behavior of the browser without the user's knowledge and consent. Doing so will serve mostly to frustrate and alienate those users. (Of course, I say this without knowing anything about your application; maybe you have a good reason for wanting this behavior.) – eaj Commented Oct 17, 2011 at 18:39
  • Your best bet is going to be picking a different shortcut key. Even if you solve this problem today, there's nothing saying a new update to Firefox wouldn't just break it again. Best to avoid the conflict altogether. – Chris Pratt Commented Oct 17, 2011 at 19:34
  • There is another case where this might actually be necessary: if the user has the "Search for text when I start typing" option enabled then the search bar will pop up for nearly any key pressed outside of a textbox. – Herohtar Commented Feb 22, 2018 at 23:23
Add a comment  | 

1 Answer 1

Reset to default 19

I agree it's important to question whether you should be using that shortcut. However, if you decide to (as others have- that's the search shortcut in gmail as well), you just need to capture the document keydown event (not keypress or keyup) and then prevent the default action, which will intercept in time to stop the default firefox behavior. Also, be sure to check that the user isn't already typing in a text field. Here's a quick example:

$(document).keydown(function(e) {
    var _target = $(e.target);
    var _focused = $(document.activeElement);
    var _inputting = _focused.get(0).tagName.toLowerCase()==="textarea" || _focused.get(0).tagName.toLowerCase()==="input";

    // / (forward slash) key = search
    if (!_inputting && e.keyCode===191) {
        e.preventDefault();
        $("#search-input").focus();
        return;
    }
});
发布评论

评论列表(0)

  1. 暂无评论