Skip Navigation

LiveSearch

The LiveSearch feature from Bitflux has inspired a fair few people to implement a LiveSearch on their own sites. The idea is simple - if a user has JavaScript, use it to show them search results as they enter their keywords, potentially saving them loading a new page to see their results.

However, the system, though very cleverly implemented, is based upon the XMLHttpRequest object, which brings with it a few problems. First, and for me most importantly, it doesn't work on Opera. I like and use Opera, so when I decided I wanted to add something similar to AddedBytes.com, that was top of the list of problems to solve.

Second, some of the versions people have come up with actually prevent a user searching normally. I want this to be a useful bonus, but not something that can stop people using the site normally under any circumstances. Of course, it goes without saying that anyone without JavaScript should not notice anything wrong or missing.

With all this in mind, I set to work. The XMLHttpRequest object method, though nicely done and perfect for the job in an ideal world, is the reason the LiveSearch was failing in Opera. Opera just doesn't support the XMLHttpRequest object. So, I ditched it. One other possibility was to use frames, which I didn't consider a serious option. Instead, I've gone for a system that makes use of JavaScript's ability to generate HTML elements on the fly and add them to a page using the DOM.

In basic terms, the LiveSearch works like this:

  • User enters a letter into the search form.
  • The browser detects the keypress and runs the LiveSearch script.
  • The LiveSearch, since it is communicating remotely with a server, first pops a "Searching ..." notice below the search box so the user knows what is happening.
  • The script polls a small PHP script on my server using the keywords entered into the search (assuming they have changed since the last LiveSearch was run).
  • The PHP script generates a JavaScript document with the results of the search in it.
  • This is added to the header of the page using the DOM.
  • The newly-added script is executed, writing the results of the search to the page.

Problems along the way were fairly constant. One that kept frustrating me was that, in Opera or Firefox, the script didn't wait for the generated JavaScript function to be added to the page before trying to run it. What that meant was that when you searched for something, it wrote out the results of your previous search. So when you entered P - H - P, pressing the second "P" triggered the script that was sent back containing the results for a search for "PH". The next keypress would trigger the function that wrote out the results for your search for "PHP". Basically, the script always wrote out the results of the previous search. Not good.

Also, some searches returned huge result sets. I didn't want to have huge files flying around, so limited results to a maximum of 10, with, where appropriate, a link to view all results.

In the end, the script works rather well. In order to always show the correct results for a search, the liveSearch function is triggered to run on a keypress, and also set to run once every two seconds independantly of keypresses, meaning that if a results file is slow to download, the search results will still be written to a page. The search itself only actually happens if the keywords are different.

As it is still very new, I'm not going to make the code available here just yet, or run through in detail how it works. However, once I am happy all is well, I'll be posting the source here for all to use.

10 comments

Doesn't seem to work in Safari. It shows the "searching ..." but results never show. I'm 80% sure it's because Safari doesn't execute JavaScript added after the page load like you are wanting to do.
Hi Sandy. I am without access to a Mac, unfortunately, so have been unable to test in Safari. I'll have a look into this though and see if there's any way to get it to work.

If as you say, Safari won't allow JavaScript to be added to a page after page load, it could be tricky to fix!
dadij
Iceland #3: October 19, 2004
The new Opera 7.60 beta supports the XMLHttpRequest object
Thanks dadij - I didn't know that.

I think, even so, that I'll probably avoid that technique for the time being. Stopping people searching normally isn't an option, and this technique will work in older browsers, which is something I'm all in favour of.
 United States #5: April 20, 2005
When is your code going to be ready? I'm very interested in seeing what you came up with.
When is your code going to be ready? I'm very interested in seeing what you came up with.
----------------
Quoted!
wanting
United States #7: November 4, 2005
Has the code been posted elsewhere? I love this solution.
Agreed, code us jack!
Trevor
United States #9: February 3, 2006
Hook it up, B.
I came here via the BitFlux wiki & your solution seems slightly nicer (with a little tweaking for my own purposes), so uhm do you feel like publishing it?

Comments Disabled

Sorry, but comments are no longer being accepted on this post. This is usually because a post has become out of date, or has become the target for an unusually high quantity of comment spam.