Created
July 8, 2011 23:01
-
-
Save niallsmart/1073037 to your computer and use it in GitHub Desktop.
makeUri.js - create a URI from an object specification
This file contains 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
// makeURI 1.2.2 - create a URI from an object specification; compatible with | |
// parseURI (http://blog.stevenlevithan.com/archives/parseuri) | |
// (c) Niall Smart <niallsmart.com> | |
// MIT License | |
function makeUri(u) { | |
var uri = ""; | |
if (u.protocol) { | |
uri += u.protocol + "://"; | |
} | |
if (u.user) { | |
uri += u.user | |
} | |
if (u.password) { | |
uri += ":" + u.password | |
} | |
if (u.user || u.password) { | |
uri += "@" | |
} | |
if (u.host) { | |
uri += u.host | |
} | |
if (u.port) { | |
uri += ":" + u.port | |
} | |
if (u.path) { | |
uri += u.path | |
} | |
var qk = u.queryKey; | |
var qs = []; | |
for (k in qk) { | |
if (!qk.hasOwnProperty(k)) { | |
continue; | |
} | |
var v = encodeURIComponent(qk[k]); | |
k = encodeURIComponent(k); | |
if (v) { | |
qs.push(k + "=" + v); | |
} else { | |
qs.push(k); | |
} | |
} | |
if (qs.length > 0) { | |
uri += "?" + qs.join("&") | |
} | |
if (u.anchor) { | |
uri += "#" + u.anchor | |
} | |
return uri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment