javascript 解析 url 的search方法
在做UI的时候 ,有时会要解析url的search部分,装了一个实用方法,共享给大家。
function searchParse(){
var resultObj = {};
var search = window.location.search;
if(search && search.length > 1){
var search = search.substring(1);
var items = search.split('&');
for(var index = 0 ; index < items.length ; index++ ){
if(! items[index]){
continue;
}
var kv = items[index].split('=');
resultObj[kv[0]] = typeof kv[1] === "undefined" ? "":kv[1];
}
}
return resultObj;
}
//Sample Code
//Url:http://localhost/default.aspx?a=hello&b=kt&c=group
var searchObj = searchParse();
var a = searchObj["a"]; //a==hello
var b = searchObj["b"]; //b== kt
var c = searchObj["c"]; //c== group