Select the following Topics you are interested
This "Javascript examplet" is extracted from article "A javascript class for retrieving query string" from Cloudgen's Javascript Laboratory
To retreive the host, path, protocol and query strings by constructing a Javascript Url Class
The following is the source code of the snippet. Click the link for downloading the snippet.
<script type="text/javascript">
function Url(a){this.setUrl(a)}
Url.prototype.setUrl=function(a){
var undefined,d,c,b,h,e,i,f,g,j;
this.queryString={};
this.url=a;
if(typeof a!="undefined") {
this.host=this.file=this.path=undefined;
if((c=a.indexOf("?"))==-1) b=a;
else {
b=a.substr(0,c);
d=((e=a.indexOf("#"))==-1?a.substr(c+1):a.substr(c+1,e-c-1)+"&"+a.substr(e+1)).split("&");
for(c=0;c<d.length;c++){
i=d[c].indexOf("=");
if(i>-1){
f=d[c].substring(0,i);
g=d[c].substring(i+1);
this.queryString[f]=unescape(g.replace(/\+/g," "))
} } }
h=b.replace(/^(https?|ftp):\/\/[^\/]+/,function(s){j=s;return ""}).split("/");
this.file=h.pop();
this.host=j;
this.path="/"+(h.length>0?h.join("/"):"");
}
}
</script>
The following is the application code of the snippet.
<script type="text/javascript">
var myUrl=new Url("http://www.google.com.hk/search?source=ig&hl=zh-TW&q=abc&meta=lr%3D&btnG=Google+Hello&aq=f&oq=");
document.write("host:"+myUrl.host+"<"+"br/>");
document.write("path:"+myUrl.path+"<"+"br/>");
document.write("file:"+myUrl.file+"<"+"br/>");
document.write(myUrl.queryString.hl+"<"+"br/>");
document.write(myUrl.queryString.source+"<"+"br/>");
document.write(myUrl.queryString.q+"<"+"br/>");
document.write(myUrl.queryString.btnG+"<"+"br/>");
</script>