尚不完善,持续修改中
html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="urlRequest.js"></script>
<script>
var urlRequestValue=new urlRequest();
console.log(document.location);
// urlRequestValue.setProtocol("http协议");
// console.log(urlRequestValue.getProtocol());
// urlRequestValue.setProtocol();
// console.log(urlRequestValue.getProtocol());
// urlRequestValue.setHost("主机地址");
// console.log(urlRequestValue.getHost());
// urlRequestValue.setHost();
// console.log(urlRequestValue.getHost());
// urlRequestValue.setPage("文件路径");
// console.log(urlRequestValue.getPage());
// urlRequestValue.setPage();
// console.log(urlRequestValue.getPage());
urlRequestValue.setFile("文件名");
// console.log(urlRequestValue.getFile());
// urlRequestValue.setFile();
// console.log(urlRequestValue.getFile());
// urlRequestValue.setSearchString("?后的参数字符串");
// console.log(urlRequestValue.getSearchString());
// urlRequestValue.setSearchString();
// console.log(urlRequestValue.getSearchString());
// urlRequestValue.setData("?后的参数对象");
// console.log(urlRequestValue.getData());
// urlRequestValue.setData();
// console.log(urlRequestValue.getData().a);
console.log(urlRequestValue.getNewUrl());
</script>
</head>
<body>
</body>
</html>
js文件
//获取url中的参数对象
function urlRequest(_url) {
var content=this;
var url = _url?String(_url):String(document.location);
var theProtocol,thePtlVal = null;//协议
var theHost,theHostVal = null; //服务器
var thePage,thePageVal = null; //文件本地地址
var theFile,theFileVal = null; //文件名
var theSearchString,theSSVal = null; //?后的数据
var theRequest = {};//?后的数据对象
//获取协议
this.setProtocol=function(_value){
thePtlVal =_value;
theProtocol=null;
if(!_value && window.location.protocol){
// if (url.indexOf("//") != -1){
// theProtocol = url.substr(0,url.indexOf("/"));
// }
theProtocol = window.location.protocol
}else{
theProtocol=_value;
}
};
this.getProtocol=function(_value){
return theProtocol;
};
//获取host
this.setHost=function(_value){
theHostVal =_value;
theHost=null;
if(!_value && window.location.host){
theHost = window.location.host;
// if (url.indexOf("//") != -1){
// var str= url.substr(url.indexOf("//")+2);
// if(str.substr(0,str.indexOf("/")).indexOf(".") != -1){
// theHost = str.substr(0,str.indexOf("/"));
// }else{
// theHost=null;
// }
// }
}else{
theHost=_value;
}
};
this.getHost=function(_value){
return theHost;
};
//获取page
this.setPage=function(_value){
thePageVal =_value;
thePage=null;
if(!_value && window.location.pathname){
thePage=window.location.pathname;
}else{
thePage=_value;
}
};
this.getPage=function(_value){
return thePage;
};
//获取file
this.setFile=function(_value){
theFileVal =_value;
theFile=null;
if(!_value && window.location.pathname){
if (window.location.pathname.lastIndexOf(".") != -1){
var str= window.location.pathname.lastIndexOf("/")+1;
theFile=window.location.pathname.substr(str);
}
}else{
theFile=_value;
}
};
this.getFile=function(_value){
return theFile;
};
//获取url中"?"后的字串
this.setSearchString=function(_value){
theSSVal =_value;
theSearchString=null;
if(!_value && window.location.search){
if (window.location.search.indexOf("?") != -1) {
var str= window.location.search.lastIndexOf("?")+1;
theSearchString=window.location.search.substr(str);
}
}else{
theSearchString=_value;
}
};
this.getSearchString=function(_value){
return theSearchString;
};
//获取url中"?"符后的字串对象
this.setData=function(_value){
theRequest = {};
if(!_value){
if (url.indexOf("?") != -1) {//判断后面有没有参数
var str = url.substr(url.indexOf("?")+1);
if(str.indexOf("#")!= -1){str=str.substr(0,str.indexOf("#"));}
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
var indexOfadd=strs[i].indexOf("=");
var key=strs[i].substr(0,indexOfadd);
if (indexOfadd>0 && key){
var value=strs[i].substr(indexOfadd+1);
if(key in theRequest){
if(theRequest[key] instanceof Array){
theRequest[key].push(value);
}else{
theRequest[key]=[theRequest[key]];
theRequest[key].push(value);
}
}else{
theRequest[key]=value;
}
}
}
}
}else{
theRequest=_value;
}
};
this.getData=function(){
return theRequest;
};
this.getNewUrl=function(_url){
content.setData();
content.setProtocol(thePtlVal);
content.setHost(theHostVal);
content.setPage(thePageVal);
content.setFile(theFileVal);
content.setSearchString(theSSVal);
var theNewUrl=theProtocol+"//"+theHost+thePage+"?"+theSearchString;
return theNewUrl;
};
}
热门评论
建议构造函数名首字母大写,参数在实例化的时候直接传入,不必分开调用。最好能一次输入,直接输出结果,简化调用操作。