JS獲取 URL相關資訊,引數
阿新 • • 發佈:2018-12-14
Location 物件包含有關當前 URL 的資訊。
Location 物件是 Window 物件的一個部分,可通過 window.location 屬性來訪問。
hash | 設定或返回從井號 (#) 開始的 URL(錨) |
---|---|
host | 設定或返回主機名和當前 URL 的埠號 |
hostname | 設定或返回當前 URL 的主機名 |
href | 設定或返回完整的 URL |
pathname | 設定或返回當前 URL 的路徑部分 |
port | 設定或返回當前 URL 的埠號 |
protocol | 設定或返回當前 URL 的協議 |
search | 設定或返回從問號 (?) 開始的 URL(查詢部分 |
採用正則表示式獲取位址列引數:
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURI(r[2]);
return null;
},
呼叫方法
var id = GetQueryString("id"); var name = GetQueryString("name");