Home > Web Front-end > JS Tutorial > js get url parameter code example sharing (JS operation URL)_javascript skills

js get url parameter code example sharing (JS operation URL)_javascript skills

WBOY
Release: 2016-05-16 17:09:23
Original
1414 people have browsed it

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.

Copy code The code is as follows:

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:

Copy code

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template