js解析url参数如http://www.taobao.com/index.php?key0=21&key1=你哈&(获取key0和key1的值)
Posted on 2013-10-11 14:49 人生梦想起飞 阅读(1591) 评论(0) 编辑 收藏 举报function parseQueryString(url) {
var pos;
var obj = {};
if ((pos = url.indexOf("?")) != -1) {
var param = url.substring(pos+1, url.length - 1)
var paramArr = param.split('&');
var keyValue = [];
for (var i = 0, l = paramArr.length; i < l; i++) {
keyValue = paramArr[i].split('=');
obj[keyValue[0]] = keyValue[1];
}
}
return obj;
}
var url = "http://www.taobao.com/index.php?key0=21&key1=你哈&";//参数后必须带一个&如:21&:你哈&
var obj = parseQueryString(url);
alert(obj.key1) // 输出你哈