Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...