function getParam(key) { var tmp = location.search; tmp = decodeURIComponent(tmp); var index = tmp.indexOf(key); var length = key.length; var value; if(index != -1){ var start = index+length+1; var end = tmp.indexOf('&',start); if(end == -1){ value = tmp.slice(start); } else{ value = tmp.slice(start,end); } } else{ value = ''; } return value; } console.log( getParam('userid') ); // xxx
js 设置url参数
setParam:function (url,key,val) { var link = url; var tmp = ''; if(link.indexOf('?') > 0 ){ tmp = link.substr( link.indexOf('?') ); // ?status=all } tmp = decodeURIComponent(tmp); link = decodeURIComponent(link); if(!tmp){ link = link + '?' + key + '=' + val + '&'; } else{ var ind = tmp.indexOf(key); if(ind < 0 ){ // 不存在这个key ind = link.indexOf('?')+1; var qianzhi = link.substr(0,ind); var houzhi = link.substr(ind); link = qianzhi + key + '=' + val + '&' + houzhi; } else{ //已有这个key,则更新这个key对应的值 ind = link.indexOf(key); var qianzhi = link.substr(0,ind); var ind2 = link.indexOf('&',ind); var houzhi = link.substr(ind2); link = qianzhi + key + '=' + val + houzhi; } } return link; },
js 移除url参数
dropParam:function (url,key) { var link = url; var tmp = ''; if(link.indexOf('?') > 0 ){ tmp = link.substr( link.indexOf('?') ); // ?status=all } tmp = decodeURIComponent(tmp); link = decodeURIComponent(link); if(tmp){ if( tmp.indexOf(key) >0){ var ind = link.indexOf(key); var qianzhi = link.substr(0,ind); var ind2 = link.indexOf('&',ind); var houzhi = link.substr(ind2+1); link = qianzhi+houzhi; } } return link; },