Add Google AJAX Search to your DNN skin: Prevent ENTER key from clearing search results

Location: BlogsAll BlogsDNN Development    
Posted by: mamlin 10/29/2008 9:12 PM

The "Add Google AJAX Search" series continues!  If you've implemented the AJAX search you probably have noticed that pressing the ENTER key will actually clear the results.  Huh?  Sure, Google AJAX doesn't need a button or ENTER keypress to perform a search, but clearing the results upon ENTER is downright confusing since people tend to hit ENTER automatically after typing in a search term.  We're going to extend our basic Google AJAX example and stop that ENTER key from clearing the search results...


ENTER to Clear (?!?)
To be perfectly clear about the issue we're trying to fix:  Google's AJAX search automatically begins searching and returning results as soon as you type in characters (try it in our Google AJAX search box on this page).  With AJAX search you do not need to use the ENTER key to submit seach terms.

Unfortunately, pressing the ENTER key results in refreshing the page which, in turn, erases the search results!  That's certainly NOT what our users would expect the ENTER key to do!  We need to stop the ENTER key from refreshing the page.


OnKeyPress to the Rescue!
Our simple solution has three steps: (1) intercept each search box keystroke, (2) check to see if keystroke is the ENTER key, (3) pass keystroke to Google search if it was not the ENTER key.

If you have not yet read Part 1 and Part 2 of the "Add Google AJAX Search" series, go back and check them out now before continuing since we'll be adding to what we've already covered.


The first step is to capture keystrokes before they're sent to Google.  We do this by adding our own OnKeyPress event handler to the search input box.  Using the input tag from part 1:

  < input id="gss_input" size="10" type="text" / >

Change the tag by adding an OnKeyPress handler to get the following:

  < input id="gss_input" size="10" type="text" onkeypress="return checkKeypress(event)" / >

 

Our handler calls a function "checkKeypress" whenever any key is pressed while focus is on the search input box.  So what does "checkKeypress" do?  Nothing, yet -- we need to add it!  What it WILL do is allow any non-ENTER keystrokes to pass through as normal while blocking ENTER keystrokes from causing postbacks and refreshing the webpage.

Again, building upon what we've already done in Part 1:  In your SCRIPT section, add the following prior to the OnLoad function:

  function checkKeypress(e)
   {  var kC  = (window.event) ? event.keyCode : e.keyCode;  // MSIE : Firefox
      var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;         // MSIE : Firefox
      if (kC == Esc) {  event.keyCode=13;  return;  }    // if ESCAPE then clear results
      if (kC == 13) return false;   // if ENTER then ignore
      return kC;   // else is regular keypress so return the keycode
   }


Block the ENTER key; Use ESCAPE to Clear

If you're paying close attention to the code you'll notice we're doing more than just stopping the ENTER key from clearing our search results.  We're also doing something with the ESCAPE key.  In fact, we're forcing the ESCAPE key to act as the ENTER key (refreshing the page thereby erasing the search results).  Using the ESCAPE key is a bit more intuitive for clearing results than using the ENTER key.  (If you'd prefer to leave ESCAPE alone simply leave out the 3rd and 4th lines of the 'checkKeypress' function.)

We use variables 'kC' and 'Esc' to help identify keycodes across different browsers.  As usual, IE handles things differently than most other browsers.  The 'window.event' lines compensate for this and allows our 'checkKeypress' function to run smoothly across different browsers.


There you have it:  a super-simple function to stop ENTER from clearing our Google AJAX Search results.  You can add to the 'checkKeypress' function to further enhance your search.  For instance, to enhance search performance you could prevent any keystroke from passing through to Google until a minimum number of search characters have been entered.  Or you could check search terms against a list of "bad words" and not allow those terms to be completed / searched.

Permalink |  Trackback

Comments (3)   Add Comment
Re: Add Google AJAX Search to your DNN skin: Prevent ENTER key from clearing search results    By Joe Craig on 11/6/2009 5:23 PM
I'd actually like to add a "search" button and not activate the Google search until it is pressed. How might I do that?

Re: Add Google AJAX Search to your DNN skin: Prevent ENTER key from clearing search results    By mamlin on 11/6/2009 5:30 PM
Hi, Joe. Between your question here and your earlier question on another of our blog entries it sounds as if you'd like to replicate the DNN search behavior of clicking "search" and getting the results in a dedicated search results page. For such a solution using the Google AJAX search I'd point you to my "comments" response to "Parag" (dated 7/28/2009 11:37 AM) on the other Google AJAX blog posting you commented on earlier today. See my 7/28/2009 response to Parag in the comments section on this page: http://www.eguanasolutions.com/DNN_Blog/EntryID/7.aspx

Re: Add Google AJAX Search to your DNN skin: Prevent ENTER key from clearing search results    By Joe Craig on 11/9/2009 5:00 PM
Thanks!


Your name:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
You are here:  
 
>> Back to the top of the blog list...

 
        account   advanced   blog   click   cloud   code   content   create   data   different   events   example   feature   file   files   free   function   good   google   just   line   list   module   modules   need   option   page   pages   query   results   role   roles   script   search   select   settings   simple   site   skin   solution   step   tags   terms   time   user   users   value   version   want  
Minimize Google AJAX Search
 
Search ES:  
This is an example of a Google AJAX Search with asynchronous search execution for two searches.  See our blog series, 'Add Google AJAX Search to your DNN skin' for info and sample code.
 
     
Minimize Get the T-Shirt
 
Shirts by Eguana Solutions
(Be cool like us!)
 
     
Minimize Most-Commented Blogs
 
 
     

Minimize Looking for more info?
 

There are tons of helpful
posts from Eguana Solutions 
on the DotNetNuke.com forums.
  
 
Click HERE to see our posts.

 
     
Minimize Modules for Sale
 

Looking for Eguana's modules? 
We're still working on them!
  

Until ours are ready to dazzle and
amaze, you'll have to make do with
the thousands of modules already
available on SnowCovered.

 
     
Minimize Favorite Modules
 

There are many great DNN modules.
A few we highly recommend are:
 
Dynamic Registration
Total control over the user signup process.  Create custom forms, execute your own SQL, use the integrated payment processing features to assign user roles, validate USERNAMEs via AJAX and much more.  Very cool.
 
URL Master
Change to friendly URLs that really ARE friendly.  Add keywords into your page URLs for better SEO.  Create 301 redirects for individual pages.  Force visitors (and search bots) to a single domain (i.e., make everyone use the "www" version of your site's URL or vice versa).  One of the single best upgrades for any DNN site.
 
Document Exchange 5 (DMX5)
Drag-and-drop from Windows Explorer directly into the DMX file manager!  File versioning, file and folder moderation, extend user permissions down to the file level (for user groups and even for individual users).  Infinite file and file info presentation options via custom display templates.  Store files locally or remotely via UNC (i.e., can securely store files somewhere besides your web server).  Much more.
 
XMOD by DNNDev
Rock-solid form module for data collection.  From simple feedback / email forms to complex, multi-part tabbed forms.  XMOD is different from other form modules because XMOD does not create a new database table for every new form definition -- an important feature if you plan to create dozens or hundreds of forms over the life of your DNN instance!  Excellent support from the developer and an active community around this module.
 
If you desire your form module to create a new DB table for each new form definition, a great alternative to XMOD is the Dynamic Forms module from DataSprings.  Dynamic Forms offers direct DB access beyond that found in XMOD as well as an easy drag-and-drop form builder option to help you get up and running very quickly.

 
     

Login