Skip to content

Instantly share code, notes, and snippets.

@brunolm
Created April 3, 2011 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunolm/900056 to your computer and use it in GitHub Desktop.
Save brunolm/900056 to your computer and use it in GitHub Desktop.
Uri object similar to .NET Uri
var www = new Uri("http://www.stackoverflow.com:80/path/sub.aspx?query=11#!/path2?query2=2");
var t = [];
for (var i in www)
{
t.push(i + " " + www[i]);
}
alert(t.join("\n"));
var Uri = function(uri) {
var parser = uri.match(/^(([^\:\/?#]+)\:)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/);
var port = parser[4].match(/\:(\d+)$/);
var segments = parser[5].split("/");
for (var i = 0; i < segments.length - 1; ++i)
{
segments[i] = segments[i] + "/";
}
this.AbsolutePath = parser[5];
this.AbsoluteUri = uri;
this.Authority = parser[4].replace(/\:\d+/, "");
this.DnsSafeHost = this.Authority;
this.Fragment = parser[8];
this.Host = this.Authority;
this.HostNameType = /^(\d|\.)+$/.test(this.Authority) ? "Ip" : "Dns";
this.LocalPath = parser[5];
this.OriginalString = uri;
this.Port = port ? port[1] : 80;
this.Query = parser[6];
this.PathAndQuery = this.AbsolutePath + this.Query;
this.Scheme = parser[2];
this.Segments = segments;
this.IsAbsoluteUri = (new RegExp("^" + this.Scheme + ":")).test(uri);
this.IsDefaultPort = this.Port == 80;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment