2017-02-24 99 views
-1

我的頁面出現問題。我已經設置了一個流程,以便每當您勾選搜索詞框時,頁面就會根據這些條款加載內容,並根據條款更改網址。歷史pushState()堆積起來

現在的問題是,我無法找到一種方法來只做網址更改的一部分。

的代碼如下:

$('input[name*=malli]').change(function(e) { 
// Code that loads the content 

data = searchterm; 

history.pushState(null, null, data); 

那麼這樣做,是讓網址

http://localhost:8000/fi/search_test 

http://localhost:8000/fi/5 

我試着加入search_test成pushState也是,但是這導致從

http://localhost:8000/fi/search_test 

http://localhost:8000/fi/search_test/5 

http://localhost:8000/fi/search_test/5/search_test/5-8 

任何幫助appriciated

+0

你們是不是要加一個查詢字符串?如果是這樣的話:https://stackoverflow.com/questions/24465030/add-query-string-using-pushstate –

回答

0

我會更好地趕路徑(即。 「/ FI/search_test /」 結尾的斜線)與:

var search_text_path = window.location.pathname || "/fi/search_test/"; 

然後

$('input[name*=malli]').change(function() { 
    data = search_text_path + encodeURIComponent(this.value); 
    history.pushState(null, null, data); 
});