Created
April 3, 2011 00:33
-
-
Save brunolm/900056 to your computer and use it in GitHub Desktop.
Uri object similar to .NET Uri
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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