Webpublisher search within search

by Peter Tyrrell Tuesday, April 22, 2008 9:46 AM

I made a working search-within-search for Inmagic Webpublisher a few months back, with the help of the wonderful mootools javascript framework, which makes object-oriented javascript easy. And fun!


The results page has a 'search within results' textbox. Search text entered there is combined with the initial search to create a brand new query.

The initial search form is serialized to a cookie, or, if the user came to the results via a GET request (canned query), the query string is parsed.

The script in action

The method which serializes a search form looks like this. Four lines: nice and neat.

   1: function SaveQuery(formObject)
   2: {
   3:     var oForm = formObject;
   4:     if (!oForm)
   5:     {
   6:         oForm = document.forms[0];
   7:     }
   8:     var formQuery = decodeURIComponent($(oForm).toQueryString());
   9:     var oQuery = new WebPublisherQuery(formQuery);
  10:     oQuery.saveToCookie(oQuery.cookieName(), {path: "/"});
  11: }

The method that handles the 'search within results' on the results page goes like this. It's a little longer to handle cookie/no-cookie, but still pretty easy to follow.

   1: function Subsearch(searchText, textbase)
   2: {
   3:     var oQuery = new WebPublisherQuery();
   4:     var isCookie = oQuery.loadFromCookie(textbase + "QueryTracker");
   5:     if (isCookie == false)
   6:     {
   7:         if (window.location.search == "")
   8:         {
   9:             return false;    
  10:         }
  11:         var windowQuery = decodeURIComponent(window.location.search.substr(1));
  12:         oQuery.loadFromQuery(windowQuery);
  13:     }
  14:     var subquery = new WebPublisherQueryGroup(searchText, {fields: oQuery.getFields()});
  15:     oQuery.addSubquery(subquery);
  16:     oQuery.saveToCookie(oQuery.cookieName(), {path: "/"});
  18:     var query = oQuery.toQueryString();
  19:     var rawUrl = window.location.href.indexOf("?") == -1 ? window.location.href : window.location.href.substring(0, window.location.href.indexOf("?")); 
  20:     window.location.href = rawUrl + query;
  21: }

Of course the real power is in the WebpublisherQuery object, which is a javascript class that does all the heavy lifting.

As with all our Webpublisher scripts, this one is released under an open source MIT license. No documentation. The mootools framework is included in the file.

Download the beta: AndornotUtilities_Subquery_0_2.js

I appreciate any feedback, bug reports, etc. We can also assist with implementation if required; contact us at: info@andornot.com.


blog comments powered by Disqus

Month List