Gravatar for wwang@asha.org

Question by wwang, Nov 17, 2017 5:42 PM

How to get the search query from jquery

For some reason, I need to force the page to reload. So I need to get the search query in order to rebuild the querystring 'q='. Is there a easy way to get the search query from search box in jquery?

2 Replies
Gravatar for erocheleau@coveo.com

Answer by Etienne, Nov 17, 2017 6:16 PM

Depending on the options on your SearchBox, you either have an Omnibox or a Querybox. Both of these components support the `getText()` methods:

https://coveo.github.io/search-ui/components/omnibox.html#gettext

https://coveo.github.io/search-ui/components/querybox.html#gettext

You should be able to do what you want with either one of those.

Now, why do you need to force the page to reload? :)

Gravatar for wwang@asha.org

Comment by wwang, Nov 17, 2017 6:21 PM

Thanks!! We need to reload the page to clear the cache. We have a Ad added right beofre the search result which won't refresh without reload.

Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Nov 17, 2017 7:46 PM

You should get more than the query when reloading the page. You should get the whole Coveo JavaScript Search Framework state (all the URL hash part). This can be done with the browser API:

var hash = window.location.hash;
Gravatar for wwang@asha.org

Comment by wwang, Nov 17, 2017 7:50 PM

I tried window.location.hash, and window.location.href. They can only return the previous search term. Not the one user currently typed in. That's why I need to rebuild the URL.

Ask a question