-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.slashSearch.js
49 lines (42 loc) · 1.6 KB
/
jquery.slashSearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Title: jQuery Slash Search plugin
* Author: Rémy Bach
* Version: 1.0.1
*
* Description:
* All this plugin does is essentially allow you to bind the '/' key to go to the first search box on your page.
* This kind of functionality is used on quite a few sites.
*
* License: MIT License - http://remybach.mit-license.org/
*/
(function($) {
var defaults = {
keyCode:191, // default to '/'
selector:'[name="search"]'
};
$.fn.slashSearch = function(options) {
// If options were passed in, merge them with the defaults.
$.extend(defaults, options || {});
// Try find the search field on the page.
var _search = $(this).find('input[type="search"]');
if (_search.length === 0) { // Ok, that didn't work - try using the selector from our options
_search = $(this).find(defaults.selector);
}
if (_search.length === 0) { // This still isn't working. Attempt to make some suggestions.
var _msg = 'Maybe try $(document).slashSearch();';
if (this.toString().match(/HTMLDocument/)) { // If they're already trying within the document scope...
_msg = 'Maybe try passing in a selector as follows: $(document).slashSearch({ selector:\'input.search\' });';
}
if (window.console && console.info)console.info('Can\'t seem to find your search field within the context specified. '+_msg);
return false;
}
this.keypress(function(e) {
// If the user isn't trying to legitimately type & the key that was pressed matches
if ($('input:focus, textarea:focus').length === 0 && e.keyCode === defaults.keyCode) {
_search.focus();
return false;
}
});
return this;
}
})(jQuery);