javascript解析url的search方法_javascript技巧
时间:2021-07-01 10:21:17
帮助过:11人阅读
代码如下:
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
如果上面的代码不够好,可以参考下面的文章。
javascript 获取url参数和script标签中获取url参数函数代码
JavaScript URL参数读取改进版
javascript[js]获取url参数的代码