A javascript class for retrieving query string





3
Date Submitted Tue. Jul. 14th, 2009 4:34 AM
Revision 1 of 1
Scripter Cloudgen
Tags Class | JavaScript | query | String | URL
Comments 0 comments
This function is quite useful in accessing the query string, host, path and filename obtained from an url. See detailed discussion in A javascript class for retrieving query string

<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>

 

Comments

There are currently no comments for this snippet.

Voting

Votes Down