Gravatar for wrolloff@gmail.com

Question by Will Rolloff, Jan 11, 2017 11:25 PM

Change searchRedirectionUrl after initialization

Is it possible to change the search redirection url after the search box has been initialized? I would like to update this based on user input.

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Jan 12, 2017 7:54 AM

Which Coveo for Sitecore version are you using (Full version number)?

Gravatar for wrolloff@gmail.com

Comment by Will Rolloff, Jan 12, 2017 8:08 AM

Version 4.0.780.0

2 Replies
Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Jan 12, 2017 9:27 AM

You could hook to the beforeRedirect event to decide which page the user should be redirected to like so:

var searchBoxElement = Coveo.$("#@Model.SearchboxId");
searchBoxElement.on(Coveo.StandaloneSearchInterfaceEvents.beforeRedirect, function(e, data) {
    // Cancel the current redirection
    data.cancel = true;

    var currentQuery = Coveo.state(searchBoxElement[0], "q");

    var myRedirectionUrl = // Your logic here

    window.location.href = myRedirectionUrl;
});

Tell us if this works for you :)

FLG

Gravatar for wrolloff@gmail.com

Comment by Will Rolloff, Jan 12, 2017 1:02 PM

I get an error that "searchBoxElement is not defined". I changed it to "Coveo.$('#@Model.SearchboxId').on(…" and the event never triggers.

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Jan 12, 2017 1:28 PM

Yes, it was expected that searchBoxElement refered to the Searchbox tag. I have edited my response to include it.

The event triggers when a document is selected or when the searchbox query is executed. In other words, when the user clicks on a document or presses enter. Does it trigger when you do any of these?

Gravatar for wrolloff@gmail.com

Comment by Will Rolloff, Jan 12, 2017 2:50 PM

There is no document to select. I am using the global search box. The event did not trigger when clicking the search icon.

Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Jan 12, 2017 1:31 PM

I have found another approach to change the redirection URI, modifying the option at runtime:

var searchBoxElement = document.getElementById("@Model.SearchboxId");
var searchBoxComponent = Coveo.get(searchBoxElement , Coveo.SearchInterface);
searchBoxComponent.options.searchPageUri = "MY_NEW_SEARCH_PAGE_URI";

The next redirection should now use that new URI.

Is this better suited for your needs?

Gravatar for wrolloff@gmail.com

Comment by Will Rolloff, Jan 12, 2017 2:54 PM

This seems like a better approach, as I could add this to the dropdown list on change event. when I try this I get an error on the following line:

var searchBoxComponent = Coveo.get(searchBoxElement , Coveo.SearchInterface);

The error is: ["ERROR", "Assert", "Assertion Failed!", "Value should not be null or undefined"]

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Jan 12, 2017 3:00 PM

I have accidentally put a # in my example before @Model.SearchboxId and have removed it. It should now work as expected.

Gravatar for wrolloff@gmail.com

Comment by Will Rolloff, Jan 12, 2017 3:40 PM

No errors this time, but it behaves a little strange. I believe the url is updated, but it does not reflect in the browser window. Also, the search term is dropped and does not show on the search landing page.

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Jan 13, 2017 8:00 AM

Changing the searchPageUri option should not affect search terms.

On the other hand, I would expect the approach with beforeRedirectto remove the search terms. Did you remove this part of the code before trying this second solution?

I think I will need more details to help you further, could you put a rundown of what you are doing and what behavior are you expecting?

Gravatar for wrolloff@gmail.com

Comment by Will Rolloff, Jan 13, 2017 10:54 AM

Page 1 has a global search box and a drop down field to indicate a category to search Page 2 is the main search page

From page 1, the drop down matches the Tabs created on the main search page. If a category is selected for the dropdown then the search should add the related tab filter to the url (e.g. http:/website/search#t=_xxxxxxxx) and when they land on the search page the search term should behave as normal while the related tab is pre-selected and the results reflect that filter.

Here is the javascript code currently added.


    
    

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Jan 13, 2017 11:03 AM

Setting searchPageUri this way results in yoururl#t=something#q=query when redirecting since it simply prepends searchPageUri with its own hash.

The best way to add the tab property would be to use the beforeRedirect event like in the first answer, and find out why it is not triggering. You could then cancel it, and append &t=something to the original URI, which would keep the original behavior and add the selected tab.

Gravatar for wrolloff@gmail.com

Comment by Will Rolloff, Jan 25, 2017 12:10 AM

Any other ideas on this?

Ask a question