The code is very simple. The main idea is to parse the url parameters into js objects, and then it is very convenient to add, delete, modify and check~, take notes here.
var LG=(function(lg){
var objURL=function(url){
this.ourl=url||window.location.href;
this.href="";//?The front part
this.params={};/ /url parameter object
this.jing="";//#and the following part
this.init();
}
//Analyze the url, get? and store it in front of this.href, Parameters are parsed into this.params objects, # and the following are stored in this.jing
objURL.prototype.init=function(){
var str=this.ourl;
var index=str.indexOf( "#");
if(index>0){
this.jing=str.substr(index);
str=str.substring(0,index);
}
Index = str.indexof ("?");
if (index & gt; 0) {
this.href = str.substring (0, index);
str = str.substr (index 1);
var parts=str.split("&");
for(var i=0;i
var kv=parts[0].split("=" );
this.params[kv[0]]=kv[1];
}
}
else{
this.href=this.ourl;
This.params ={};
}
}
//Just modify this.params
objURL.prototype.set=function(key,val){
this.params[key]=val;
}
//Just set this.params
objURL.prototype.remove=function(key){
this.params[key]=undefined;
}
//According to The three parts constitute the url after the operation
objURL.prototype.url=function(){
var strurl=this.href;
var objps=[];//Array organization is used here, and then the join operation is performed
for(var k in this.params){
if(this.params[k]){
objps.push(k "=" this.params[k]);
}
out out out of (
strurl =this.jing;
}
return strurl;
}
//Get parameter value
objURL.prototype.get=function(key){
return this .params[key];
}
lg.URL=objURL;
return lg;
}(LG||{}));
LG is just my personal common JS namespace, nothing else. Call: