2012-05-04 71 views
1

我正在使用以下代碼將param附加到url。這工作正常,但當參數被添加到網址,頁面正在重新加載。我想在不重新加載頁面的情況下使用此功能。在URL中追加參數,無需重新加載頁面

function insertParam(key, value) 
{ 
    key = escape(key); value = escape(value); 

    var kvp = document.location.search.substr(1).split('&'); 


    var i=kvp.length; var x; while(i--) 
    { 
     x = kvp[i].split('='); 

     if (x[0]==key) 
     { 
       x[1] = value; 
       kvp[i] = x.join('='); 
        alert('sdfadsf'); 
       break; 
     } 
    } 

    if(i<0) {kvp[kvp.length] = [key,value].join('=');} 

    //this will reload the page, it's likely better to store this until finished 
    document.location.search = kvp.join('&'); 
    //alert(document.location.href); 

}

我想添加多個PARAMS到URL,而不重新加載頁面,如:

  • TXT1
  • TXT2
  • txt3
  • LINK1
  • 鏈接2
  • LINK3

我想網址: 「....../search.php中」

點擊TXT2 後,我想網址:」 .... /search.php#t_2"

點擊鏈接2 後,我想網址:」 ....../search.php中#T_1 & L_2"

+0

我想補充複式參數一樣網址: –

回答

5

您只能使用history.pushState(state, title, url)這是一個HTML5功能。

+0

我覺得OP只想追加片段的URL,例如'document.location.href + =「# txt'' – Nadh

+0

@NADH他從未說過 - 代碼(和原始問題)討論了查詢參數,而不是錨點片段。 – Alnitak

+0

原來我誤讀,我的壞! – Nadh

相關問題