题解 | #获取 url 参数#
获取 url 参数
http://www.nowcoder.com/practice/a3ded747e3884a3c86d09d88d1652e10
function getUrlParam(sUrl, sKey) { let query = sUrl.split('?')[1] let params = {} query.split('#')[0].split('&').forEach(item=>{ let key = item.split('=')[0] let value = item.split('=')[1] if(!params[key]){ params[key] = value }else if( params[key] instanceof Array){ params[key].push(value) }else{ params[key] = [params[key],value] } }) if(!sKey){ return params }else{ return params[sKey]?params[sKey]:'' } }