2016-09-16 85 views
0

我試圖在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> &gt; " + $("[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組件?

+0

錯誤告訴你他們在哪裏被拋出。這段代碼中的錯誤是在哪裏產生的?我懷疑它涉及到傳遞到jQuery選擇器。你爲什麼使用'encodeURIComponent()'來處理來自url的內容?如果你把這個縮小到最小表示形式 – charlietfl

+0

感謝你的迴應@charlietfl - 控制檯錯誤只是列出了'jquery.js?ver = 1.12.4:2',那麼不會是插件腳本本身。我自己並沒有真正寫這個腳本,所以我不確定爲什麼使用'encodeURIComponent()' - 我應該嘗試從'reqId'和'hashParts'變量中刪除這個函數嗎? – nickpish

+1

對我來說編碼沒有任何意義,而如果你將它傳遞給選擇器,則需要解碼。另一種可能是使用jQuery'filter()'而不是使用選擇器。 – charlietfl

回答

1

用法?而不是#

site.com/jobs/all-positions?country=united-states&city=minneapolis 
+0

謝謝Art--你爲什麼會認爲'?'不會導致在錯誤中嗎?我只是用'var filterHash ='?'替換'var filterHash ='#';' – nickpish

+0

@nickpish hashtag是錨定標記,並且始終位於URL中的最後一個。指示查詢字符串。關於URL結構的更多信息:[link](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL) –

+0

好的,所以你認爲在開始時使用'#'可能會導致其餘部分參數不能被腳本正確解析/識別? – nickpish