javaScript 客戶端中獲取url的傳參數(shù)
GetQueryString(name)?
name 為參數(shù)的變量
function GetQueryString(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return? unescape(r[2]); return null;
}
例子:
url: a.html?id=123
console.log(GetQueryString("id"));
function GetQueryString(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return? unescape(r[2]); return null;
}
輸出:123
延伸:
用該屬性獲取頁(yè)面 URL 地址:
window.location 對(duì)象所包含的屬性
屬性描述
hash :從井號(hào) (#) 開(kāi)始的 URL(錨)
host : 主機(jī)名和當(dāng)前 URL 的端口號(hào)
hostname : 當(dāng)前 URL 的主機(jī)名
href : 完整的 URL
pathname : 當(dāng)前 URL 的路徑部分
port : 當(dāng)前 URL 的端口號(hào)
protocol : 當(dāng)前 URL 的協(xié)議
search : 從問(wèn)號(hào) (?) 開(kāi)始的 URL(查詢部分)
console.log(window.location.search);? 輸出 ?id=123
console.log(window.location.search.substr);? 輸出id=123?
match(reg) :表示為 正則表達(dá)式?