I'm trying to debug a script on a WordPress site that that allows for dynamic filtering of job listings (via Jobvite.) When the page is initially loaded without any URL parameters attached-- e.g. site.com/jobs/all-positions
-- the filters work fine; however, when the page is loaded/reloaded with parameters attached--e.g. site.com/jobs/all-positions#country=united-states&city=minneapolis
, the filters don't work at all, and I see the following jQuery error in the console:
Syntax error, unrecognized expression:
#country%3Dunited-states%26city%3Dminneapolis
It seems that the URI components are not being properly encoded (?) and I'm wondering if there's an error I'm not seeing in the plugin script that handles the filtering, which I'm including below:
// Update Filter hash
function updateFilterHash() {
var filterHash = '#';
if ( selectedCountryFilters.length !== 0 ) {
filterHash += "country=" + selectedCountryFilters.join(",") + "&";
}
if ( selectedCityFilters.length !== 0 ) {
filterHash += "city=" + selectedCityFilters.join(",") + "&";
}
if ( selectedCategoryFilters.length !== 0 ) {
filterHash += "category=" + selectedCategoryFilters.join(",") + "&";
}
console.log(filterHash);
$.History.go( filterHash );
}
// Set page state from URL hash
if ( location.hash.length !== 0 ) {
// Set Req detail view
if ( location.hash.indexOf("#req-") !== -1 ) {
var reqId = encodeURIComponent(location.hash.slice(1));
$(".all-requisitions").hide();
$(".all-open-positions-title").hide();
$(".breadcrumbs").append( "<span> > " + $("[data-id='" + reqId + "'] h1").text() + "</span>" );
$(".requisition-detail[data-id='" + reqId + "']").fadeIn();
}
// Set Filters
if ( location.hash.indexOf("&") !== -1 ) {
var hashParts = encodeURIComponent(location.hash.slice(1)).split("%26");
// Activate Filters
for (var i = 0; i < hashParts.length; i++) {
if ( hashParts[i] !== "" ) {
var hashPairs = hashParts[i].split("%3D");
hashName = hashPairs[0],
hashValues = hashPairs[1].split("%2C");
for (var j = 0; j < hashValues.length; j++) {
$("a[href='#" + hashName + "-" + hashValues[j] + "']").addClass("selected");
// Update appropriate Filter arrays and perform other Filter-specific actions
switch (hashName) {
case "country":
selectedCountryFilters.push( hashValues[j] );
// If Filter is a Country, reveal the Cities too
updateCityFilters();
break;
case "city":
selectedCityFilters.push( hashValues[j] );
// If Filter is a city, activate the Country too
var hashCountry = $("a[href='#" + hashName + "-" + hashValues[j] + "']").data("country");
$("a[href='#country-" + hashCountry + "']").addClass("selected");
selectedCountryFilters.push( hashCountry );
break;
case "category":
selectedCategoryFilters.push( hashValues[j] );
break;
default:
break;
}
}
}
}
updateReqs();
updateCategoryHeadings();
}
}
This is just the section of the script that handles the URI hash components, and I'm thinking the issue must lie somewhere here. What's truly frustrating, is that the page/script works perfectly fine on a testing server (and therefore on a different WordPress install) but not on the staging server. However, both have the same versions of WP and are loading the same versions of jQuery. Any ideas here as to why the script would not be properly parsing the URI components?