|
|
HistoryInspired by Apples upcoming Spotlight feature (and others, Apple just inspired me to finally do it...), I implemented a LiveSearch function into our blog. Go to http://blog.bitflux.ch/ and type something into the right search box. It should immediately show the searches found. How it worksOn the client side, we use XMLHttpRequest for sending the request to the server. There we have a little PHP script, which returns a small HTML file (http://blog.bitflux.ch/livesearch.php?s=PHP ) . This is then inserted into the page with some DOM manipulation. The drop shadow is done with some CSS magic (see http://wubbleyew.com/tests/dropshadows.htm ). Supported Browsers
Not supported
Source CodeThe code is licensed under the Apache Software License 2.0 and can currently be found under http://svn.liip.ch/repos/public/fluxcms/branches/1_5/webinc/js/livesearch.js. InstallationGrab yourself a copy of the js script. Open it and search for 'livesearch.php?q'. Change the filename and variable according to the ones you plan to use. Now on the html page in which you wish to provide the feature you'll have to: link the script in the <head> section; add an onload="liveSearchInit()" to the body tag; create a form named "searchform" having a handler _ onsubmit="return liveSearchSubmit()". Add an input field with _id="livesearch", name="q" and onkeypress="liveSearchStart()". Then in the point where the result are to be displayed insert these two divs as placeholders: <div id="LSResult" style="display: none;"><div id="LSShadow"></div></div> At this point all that is missing is the script on the server which will respond to search requests. Using your preferred language write down a simple page for querying the database and retrieving matching records against the string contained in the above cited variable ('q'). Resulting output has to be a text/xml file (check http://blog.bitflux.ch/livesearch.php?q=PHP as a reference) in the form of a parent <div class="LSRes"></div>, containing one more <div class="LSRow"></div> for each record: inside the latter put whatever code had to be displayed (typically a link). ContributionsSince the code is open source, we're glad to accept your improvements you made to the code. Send patches to chregu@bitflux.ch IframesThe same would be possible with iframes, but XMLHttpRequest gives you more control about the actual request. Actually, I believe you can post a form into an iframe. It takes a little more work, but it is still doable. You could even dynamically attach the iframe, build the form, point it at the iframe (of course, this would be "verboten" in Strict - no target), and submit it. Demos
Links
QuestionsIf you have questions, please use the Discussion link above and add your question or comments Future Ideas
|
7 Comments
Hide/Show CommentsDec 21, 2007
Anonymous
Konqueror browser (KHTML rendering in fact) works at least since 3.5.8 version)
Mar 09, 2008
Anonymous
Hi there,
Please feel free to delete this comment if deemed necessary but I was just wondering how do you get the Live Search to work so that when the results come up you can use the up / down and enter keys to select an item? At the moment I can get it to spit out the results but I have to click on them by hand. Would love to be able to use the arrow keys to select instead.
Any help with this would be massively appreciated.
Best wishes,
Mark
Sep 24, 2008
Anonymous
I need some help.
I have Problems with this step:
"At this point all that is missing is the script on the server which will respond to search requests. Using your preferred language write down a simple page for querying the database and retrieving matching records against the string contained in the above cited variable ('q'). Resulting output has to be a text/xml file (check http://blog.bitflux.ch/livesearch.php?q=PHP as a reference) in the form of a parent <div class="LSRes"></div>, containing one more <div class="LSRow"></div> for each record: inside the latter put whatever code had to be displayed (typically a link)."
How does it works? Does anybody have a little example for me?
I wanna use livesearch not as a normal search. I have a very long Dropdown and I want to replace it with a livesearch input field.
Only the values from the former dropdown should be a result of the livesearch input field. Is this possible? If yes, how?
Thanks.
Stefan
Oct 08, 2008
Anonymous
Is there a way to make backspace work? Looks like pressing backspace does not update the results.
Oct 08, 2008
Anonymous
Looks like backspace works in Firefox, but not Safari. Is it a Safari/WebKit bug?
Oct 15, 2008
Anonymous
I found that this does not work in IE7 quite as it should. only the initial submission works. - the reason is that while IE7 does support window.XMLHttpRequest it is only IE6 dressed up, so like IE6 it needs a new request each time. Fortunately it still supports ActiveXObject so you can use this to test for IE7
Therefore, in the function 'liveSearchDoSearch()' I changed the empty
if (window.XMLHttpRequest) {
}
to
if (window.XMLHttpRequest && window.ActiveXObject)
else if (window.ActiveXObject)
This will get it working in IE7.
cheers,
Niall.
Dec 30, 2009
Anonymous
What is document.getElementById("LSHighlight"); ????
LS HighLight is not a XMl-tag in this example?