獲取URL參數(shù)
getUrlParam:function(name){
var reg=new RegExp('(^|&)'+name+'=(&*)(&|$)');
var result=window.location.search.substr(1).match(reg);
return result ? decodeURIComponent(result[2]):null;
}
說明:
- reg是正則表達(dá)式子
- location是包含了相關(guān)的url的信息,它是windown的一部分。
- search是一個可以查詢的屬性,可以查詢?之后的部分。
- match()匹配函數(shù)
- return unescpe(r[2]) 返回的值 一個數(shù)組或者null
- decodeURIComponent() 函數(shù)可對 encodeURIComponent() 函數(shù)編碼的 URI 進(jìn)行解碼。
調(diào)用
console.log(getUrlParam('test'));