JS獲取獲取當前頁面的參數值
利用JavaScript自帶參數變量
Location 對象屬性
屬性 |
描述 |
hash |
設置或返回從井號 (#) 開始的 URL(錨)。 |
host |
設置或返回主機名和當前 URL 的端口號。 |
hostname |
設置或返回當前 URL 的主機名。 |
href |
設置或返回完整的 URL。 |
pathname |
設置或返回當前 URL 的路徑部分。 |
port |
設置或返回當前 URL 的端口號。 |
protocol |
設置或返回當前 URL 的協議。 |
search |
設置或返回從問號 (?) 開始的 URL(查詢部分)。 |
/**
* 獲取瀏覽器url中的參數
*/
function getUrlParam(name) {
var zhengze= new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //正則匹配 (^|&) 開頭 (&|$)以&結尾
var para = window.location.search.substr(1).match(zhengze);
if (para != null) {
return decodeURIComponent(para [2]); // 也可以 這樣寫 unescape(para [2]);
}
return '';
}
//獲取ServiceId參數的值
var ServiceId = getUrlParam("id");