This is a followup to my earlier question How can I get tabbed site and image search results using Google Programmable Search?
I’m now trying to add date and sort parameters to the two URLs as variables that retrieve the search results. One URL retrieves web results for the web tab, the other image result for the images tabs. With the output of the Select dropdown onchange event, I need to append the sorting parameters to the two search and image URLs and reload them both.
This is the select dropdown
<select id="selectsort" onchange="selectthesort()">
<option value="">Sorted by relevance</option>
<option value="&sort=date">Sort by date</option>
<option value="&dateRestrict=d14&sort=date">Last two weeks by date</option>
</select>
that by default option value (Sort by relevance) does not append to the URLs, since search results are by default already sorted by relevance by Google.
But for the other two options, I need to append either &sort=date to the URL when “Sort by date” is selected, or &dateRestrict=d14&sort=date when “Last two weeks by date” is selected, to each of these two URL variables:
One is for the web tab:
jsElm.src = "https://www.googleapis.com/customsearch/v1?key=key&cx=cx&start="+start+"&q="+query+"&callback=hndlr";
And this one is for the Images tab:
jsElmIM.src = "https://www.googleapis.com/customsearch/v1?key=key&cx=cx&start="+start+"&q="+query+"&searchType=image&callback=hndlrimages";
I.e. I need to do this:
When “Sort by date” is selected, append &sort=date to the jsElm.src URL
jsElm.src = "https://www.googleapis.com/customsearch/v1?key=key&cx=cx&start="+start+"&q="+query+"&callback=hndlr";
to get this:
jsElm.src = "https://www.googleapis.com/customsearch/v1?key=key&cx=cx&start="+start+"&q="+query+"&callback=hndlr&sort=date";
And append the same to the jsElmIM.src URL.
I’ve already got
var sortoptions;
function selectthesort(){
sortoptions = document.getElementById('selectsort').value;
console.log(sortoptions);
}
and that outputs the strings I need to append to the two search URLs.
Would I concatenate variables? Or append? Or use some other method?
And then how do I “fire” the URL to update/reload?
