<script type="text/javascript">
var str = "https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=0";
function getUrl(str) {
str = str.slice(str.indexOf("?") + 1); //结果:ie=utf-8&f=3&rsv_bp=0
var arr = str.split("&"); //结果: ["ie=utf-8", "f=3", "rsv_bp=0"]
var obj = {};
for(var i = 0; i < arr.length; i++) {
var newArr = arr[i].split("="); //结果:["ie", "utf-8"],["f", "3"],["rsv_bp", "0"]
obj[newArr[0]] = newArr[1];
}
return obj;
}
console.log(getUrl(str)); // 结果:{ie: "utf-8", f: "3", rsv_bp: "0"}
</script>
js解析url中的字符串
最新推荐文章于 2023-10-14 09:30:45 发布