之前在博客中写过,window.location.href的各个字段的值和属性,见博客https://blog.csdn.net/YJD19970908/article/details/81189583,这这种有个方法 window.location.search.比如
//url是 https://www.baidu.com/?a=1&b=2
console.log(window.location.search) //?a=1&b=2
那么如何简单快捷的拿到a的值或者b的值呢?
function getParaByName(name) {
var search = window.location.search;
search = search.substr(1);
if (typeof name === 'undefined') return search;
var searchArr = search.split('&');
for (var i = 0; i < searchArr.length; i++) {
var searchStr = searchArr[i];
searchArr[i] = searchStr.split('=');
if (searchArr[i][0] == name) {
return searchStr.replace(name + '=', '');
}
}
return '';
}
{
"name":"Jontyy" ,
"email": " jontyy@163.com"
}