Spice up your Drupal 6 site search field with a bit of jQuery and CSS
cssdrupalfirefoxjavascriptjqueryplanetdrupalsafarisearchDevelopment
The search field is an important part of almost any web site today and it deserve some attention. Especially if you have implemented some really good search like Apache Solr (see How to set up Apache Solr search for Drupal 6 on a Debian GNU/Linux server).
This is an update to an older post on the same subject but for Drupal 5. This new version is for Drupal 6 and adds jQuery 1.3+ compatibility plus some nice CSS for Firefox.
If you use Safari, or any other Webkit browser, the spiced up search field looks like a standard Mac OS X search field:
With the help of some CSS I made the spiced up Firefox search field look somewhat similar:
All this is done with JavaScript and CSS on the original Drupal search form ensuring graceful degradation. If you turn of JavaScript all that happens is that you get the standard search field and button.
Here follows the code I use to implement this here on xdeb.org.
I almost always have a site specific module that I name “[sitename]_addon” where I put all the custom bit and pices of code that the site needs. The search field is usally on every page so I use hook_init() to add the JavaScript and CSS file. Here I also insert the text string that I want to appear in the search field as a placeholder/hint. Doing it this way makes it possible to use the Drupal locale module to translate it.
In all the code below you should replace “example” with something that suits you, the site name for example.
/**<br> * Implementation of hook_init().<br> */<br>function example_addons_init() {<br> global $base_url;<br> $parts = parse_url($base_url);<br> $host = array_reverse(explode('.', $parts['host']));<br><br> $path = drupal_get_path('module', 'example_addons');<br> drupal_add_css($path .'/example_addons.css', 'theme');<br> drupal_add_js($path .'/example_addons.js');<br> drupal_add_js(array('example' => array(<br> 'search_hint' => t('Search on @site', array('@site' => variable_get('site_name', 'Drupal')))),<br> 'search_domain' => $host[0] .'.'. $host[1] .'.search',<br> ), 'setting');<br>}
Here is the CSS file.
/* $Id: $ */<br><br>/* Text hints in search fields */<br>.texthint {<br> color: #999;<br>}<br><br>/* Safari style search field for Firefox */<br>#search .safaristyle,<br>#search-block-form .safaristyle {<br> -moz-border-radius: 1em;<br> -moz-box-shadow: 0 0 2px #ccc inset;<br> border: 1px solid #999;<br> padding: 2px 0.5em;<br>}<br><br>/* Stop search label and button to flicker before js is loaded. */<br>.js #search label,<br>.js #block-search-0 label,<br>.js #search #edit-submit,<br>.js #search #edit-submit-1,<br>.js #block-search-0 #edit-submit,<br>.js #block-search-0 #edit-submit-1 {<br> display: none;<br>}
Here is the jQuery JavaScript file.
In all the code below you should replace “example” with something that suits you, the site name for example.
// $Id: $<br>(function ($) {<br><br>$.fn.texthint = function (title) {<br> return this.each(function () {<br> // Get jQuery version of 'this'.<br> var t = $(this);<br> // Only apply logic if the element has the attribute.<br> if (title) {<br> t.addClass('texthint').attr('size', '20');<br> if ($.browser.mozilla) {<br> t.addClass('safaristyle');<br> }<br> // On blur, set value to title attr if text is blank.<br> t.blur(function () {<br> if (t.val() == '') {<br> t.addClass('texthint').val(title);<br> }<br> });<br> // On focus, set value to blank if current value<br> // matches title attr.<br> t.focus(function () {<br> if (t.val() == title) {<br> t.removeClass('texthint').val('');<br> }<br> });<br><br> // Clear the pre-defined text when form is submitted.<br> t.parents('form:first').submit(function () {<br> if (t.val() == title) {<br> t.removeClass('texthint').val('');<br> }<br> });<br><br> // Now change all inputs to title.<br> t.blur();<br> }<br> });<br>}<br><br>Drupal.behaviors.initSearchTexthint = function (context) {<br> $('input#edit-search-theme-form-1, input#edit-search-block-form-1', context).each(function () {<br> var input = $(this);<br> var settings = Drupal.settings.example;<br> if ($.browser.safari) {<br> // jQuery 1.2 does no longer allow changing the type property.<br> input.get(0).type = 'search';<br> input.attr({<br> autosave: settings.search_domain,<br> results: '9',<br> placeholder: settings.search_hint,<br> size: '20'<br> });<br> }<br> else {<br> input.texthint(settings.search_hint);<br> }<br> });<br>}<br><br>})(jQuery);
AttachmentSize safari_search_field_2.png1.98 KB firefox_search_field_2.png2.31 KB