我试图在WordPress网站上调试脚本,该脚本允许动态过滤作业列表(通过Jobvite)。当页面最初加载时没有任何URL参数 - 例如site.com/jobs/all-positions
- 过滤器正常工作;然而,当页面被加载/重载时与附加的参数 - 例如, site.com/jobs/all-positions#country=united-states&city=minneapolis
,该过滤器不会在所有的工作,而我在控制台看到下面的jQuery的错误:从URL散列设置页面状态 - 语法错误(jQuery)
语法错误,无法识别的表达:
#country%3Dunited-states%26city%3Dminneapolis
看来,URI成分不被正确编码,我想知道如果有,我不是在处理过滤,我包括低于该插件脚本看到一个错误(?):
// 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();
}
}
这是脚本的只是部分那把手是URI哈希组件,我认为这个问题一定在这里。真正令人沮丧的是,页面/脚本在测试服务器上工作得很好(因此在不同的WordPress安装上),但不在登台服务器上。但是,两者都具有相同版本的WP,并且正在加载相同版本的jQuery。这里的任何想法,为什么脚本不会正确解析URI组件?
错误告诉你他们在哪里被抛出。这段代码中的错误是在哪里产生的?我怀疑它涉及到传递到jQuery选择器。你为什么使用'encodeURIComponent()'来处理来自url的内容?如果你把这个缩小到最小表示形式 – charlietfl
感谢你的回应@charlietfl - 控制台错误只是列出了'jquery.js?ver = 1.12.4:2',那么不会是插件脚本本身。我自己并没有真正写这个脚本,所以我不确定为什么使用'encodeURIComponent()' - 我应该尝试从'reqId'和'hashParts'变量中删除这个函数吗? – nickpish
对我来说编码没有任何意义,而如果你将它传递给选择器,则需要解码。另一种可能是使用jQuery'filter()'而不是使用选择器。 – charlietfl