Gravatar for chris.ruderschmidt@americaneagle.com

Question by Chris Ruderschmidt, Sep 1, 2015 7:37 PM

Is there a way to reference the search term?

Just looking for a way to reference the search term on the results page and place it elsewhere on the page.

eg:

<h2>you searched for <%=SEARCH_TERM%></h2>

Thanks!

1 Reply
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Sep 2, 2015 9:49 AM

The search terms are not available on the server-side when the Coveo JavaScript Search Framework is rendered in a Coveo for Sitecore search page. Also, the search terms could (and will likely) change during the search page lifetime as the user refines his query to find what he is looking for. Because of that, it is recommended to implement this feature in JavaScript on the client side.

  1. You could implement a custom component to do this. It could listen for state changes for the "q" state parameter and display it when it changes (see Implementing a Custom Component).
  2. You could create an HTML element with a unique id attribute value. Then, in JavaScript, you could add an event handler on the duringQuery, preprocessResults or querySuccess event to update your HTML element inner text with the "q" state parameter value.
Gravatar for chris.ruderschmidt@americaneagle.com

Comment by Chris Ruderschmidt, Sep 2, 2015 10:43 AM

I believe your answer is what I'm looking for, but just for completion sake, is this the jQuery line of code to reference it?

$("#search").coveo('state', 'q')

Thanks!

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Sep 2, 2015 10:57 AM

Yes, this line of code is the one to read the "q" state parameter of the Coveo JavaScript Search Framework jQuery plugin.

Ask a question