Home > Web Front-end > JS Tutorial > body text

Modified version of Javascript operation URL function_javascript skills

WBOY
Release: 2016-05-16 17:16:55
Original
1240 people have browsed it

Summarized some methods and compressed it into a function object with only a few dozen lines of code. The usage method is simple and clear
Save as ojbUrl.js

Copy code The code is as follows:

/*
Note: This code can be copied, modified and used freely, but please keep the author information!
Author: Kevin WebSite: http://iulog.com/ QQ:251378427
JS operation URL function usage instructions:
Initialize var myurl=new objURL(); //You can also customize the URL: var myurl=new objURL('http://www.jb51.net');
Read the url parameter value var val=myurl.get('abc'); // Read the value of the parameter abc
Set url parameter myurl.set("arg",data); //Add/modify the value of an arg parameter to data
Remove url parameter myurl.remove("arg"); //Remove arg parameter
Get the processed URL myurl.url();//Generally, execute the jump directly location.href=myurl.url();
Debugging interface: myurl.debug(); //Modify this function for debugging
*/
function objURL(url){
var ourl=url||window.location.href;
var href="";//?Front part
var params={} ;//url parameter object
var jing="";//#and the following parts
var init=function(){
var str=ourl;
var index=str.indexOf(" #");
if(index>0){
jing=str.substr(index);
str=str.substring(0,index);
}
index=str .indexOf("?");
if(index>0){
href=str.substring(0,index);
str=str.substr(index 1);
var parts =str.split("&");
for(var i=0;i var kv=parts[i].split("=");
params[kv[0]]=kv[1];
}
}else{
href=ourl;
params={};
}
};
this.set=function(key,val){
params[key]=encodeURIComponent(val);
};
this.remove=function(key){
if(key in params) params[key]=undefined;
};
this.get=function(key){
return params[key];
};
this.url=function(key){
var strurl=href;
var objps=[];
for(var k in params){
if(params[k]){
objps.push(k "=" Params [k]);
}
}
if (objps.length & gt; 0) {
strurl = "?" objps.Join ("&"); if(jing.length> Free settings
var objps=[];
for(var k in params){
objps.push(k "=" params[k]);
}
alert(objps) ;//Output all values ​​of params
};
init();
}

Call method
JS operation URL function usage instructions:
[code]
var myurl=new objURL(); //Initialization. You can also customize the URL: var myurl=new objURL('http://www.jb51.net'');
var val=myurl.get('abc'); // Read the value of parameter abc
myurl.set("arg",data); // Add/modify the value of an arg parameter to data
myurl.remove("arg"); //Remove the arg parameter
myurl.url ();//Get the processed URL, usually jump directly: location.href=myurl.url();
myurl.debug(); //This is the debugging interface. Modify this function method for debugging

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