js获取url参数,操作url参数

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('?') > ){
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 < ){ // 不存在这个key
ind = link.indexOf('?')+;
var qianzhi = link.substr(,ind);
var houzhi = link.substr(ind);
link = qianzhi + key + '=' + val + '&' + houzhi;
}
else{ //已有这个key,则更新这个key对应的值
ind = link.indexOf(key);
var qianzhi = link.substr(,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('?') > ){
tmp = link.substr( link.indexOf('?') ); // ?status=all
}
tmp = decodeURIComponent(tmp);
link = decodeURIComponent(link);
if(tmp){
if( tmp.indexOf(key) >){
var ind = link.indexOf(key);
var qianzhi = link.substr(,ind);
var ind2 = link.indexOf('&',ind);
var houzhi = link.substr(ind2+);
link = qianzhi+houzhi;
}
}
return link;
},
上一篇:part4:素数判别


下一篇:UVA 11542 高斯消元