2013-11-01 13 views
-1

我怎樣才能得到額外的獲取參數在js中的href? 我想刪除url中的refer_id = xxx,然後爲它添加一個新的refer_id = xxx get參數。該網址不是唯一的。
有沒有快速的方法來做到這一點?我怎樣才能得到額外的獲取參數在js中的href?

// http://www.newpm.com/ 
// http://www.newpm.com/?refer_id=100 
// http://www.newpm.com/index.php?main_page=product_info&refer_id=100 
// http://www.newpm.com/index.php?main_page=product_info&refer_id=100&from_home 
// http://www.newpm.com/index.php?refer_id=100&from_home 

例如:

// http://www.newpm.com/index.php?main_page=product_info&refer_id=100&from_home 

更換refer_id = XXX GET:

// http://www.newpm.com/index.php?main_page=product_info&&from_home 

替換URL具有 「& &」,然後添加一個新refer_id

// http://www.newpm.com/index.php?main_page=product_info&from_home&refer_id=200 
+0

使用'window.location.search =「queryString」'。查詢字符串表示URL中'?'後面的內容。 – SeinopSys

+1

目前還不清楚你問的是什麼問題。你在問如何從當前頁面的URL中解析查詢參數嗎?或者你問如何使用javascript在頁面中的現有鏈接上更改href。 – jfriend00

+0

@ DJDavid98不是100%準確......'alert(location.search)'會返回'?' – charlietfl

回答

2

以及在一般情況下,這是你將如何取代它..

var newVal = 'value here'; 
yourLink = yourLink.replace(/([&?]refer_id=)[^&#]*/,'$1'+newVal); 

現在,你沒上過明確的是你想要哪個URL來代替。您是否想要替換實際網頁網址中的參數,例如瀏覽器欄中的網址?或者您是否想要替換錨標記中的href屬性?或者你已經有一些變量的網址?上面代碼的原理基本上是相同的,儘管yourLink變量你的行爲取決於你想要改變什麼