I ran across a surprising little feature in Safari. If you change a standard:
<input type="text" />
field to a “search” box like so:
<input type="search" />
In Safari, it is magically transformed from a standard input field to a “search box”. It can also have added functionality like remembering previous searches with an extra attribute:
<input type="search" results="5" />
Here is a screenshot:
and here is a live demo page.
This seems to degrade gracefully as a normal text field as well. Tested in:
- Safari (of course)
- Camino
- Firefox
- IE 6
- IE 7
- Opera 9
This, of course, is not valid XHTML. However, it could be hacked around with a little JavaScript for Safari only. I’ve done so for the search box in the upper right column on my site using the following JavaScript code:
if (safari) {
var s = document.getElementById('s');
s.type = 'search';
s.setAttribute('results', '5');
}
Seems to work just fine.
UPDATE: for those of you not on a Mac, here is a screenshot of it in Safari:
[…] Safari Search Boxes: […]
I’m copying the snippet of code verbatim and it doesn’t seem to work for me. Is there a script that I need in order for the Safari-only JS to work? Great resource!
Dru, you’d need to give the field an id of ‘s’.
e.g:
Well I have saffari 4 for windows vista and theses searches work fine in windows !
I’m on a Mac, but the new Safari 4 does not show a Safari-like search box on the top-right of this very site…
Probably due to a recent change in javascript includes, the demonstrated search box doesn’t work at all. You should put :
if (safari) { var s = jQuery(‘#s’); s[0].type=’search’; s.attr(‘results’, ‘5’); }
instead. Cheers!
Yep, thanks – patch applied.
That is one of the slickest little hacks I’ve seen.