Getting back to ajax search results from a page

I solved this using the HistoryAPI.

Since my ajax returns objects that has response, html and query, I pushed the html results of the search to the history state with

window.history.pushState( date_out.html, null, null);

where data_out is the object returned from the ajax call.

Then I added a button on the single advert page that will do

window.history.back();

on click. This will send me to the search results page. Then I can check the url of the search page (this can be hardcoded or pulled from the localization object where it’s set with WordPress), and if there is anything in the history.state which corresponds to the search html I just place it after the desired div.

Then inside that check I also push the empty state in the history just to be sure that when you go to some other page and push back that you won’t see any search results.

Seems to be working fine 🙂