URI.js
I always want to shoot myself in the head when looking at code like the following:
var url = "http://example.org/foo?bar=baz",
separator = url.indexOf('?') > -1 ? '&' : '?';
url += separator + encodeURIComponent("foo") + "=" + encodeURIComponent("bar");
I still can't believe javascript - the f**ing backbone-language of the web - doesn't offer an API for mutating URLs. Browsers (Firefox) don't expose the Location
object (the structure behind window.location). Yes, one could think of decomposed IDL attributes as a native URL management library. But it relies on the DOM element <a>, it's slow and doesn't offer any convenienve at all.
How about a nice, clean and simple API for mutating URIs:
var url = new URI("http://example.org/foo?bar=baz");
url.addQuery("foo", "bar");
URI.js is here to help with that.
API Example
URI("http://example.org/foo.html?hello=world")
.username("rodneyrehm")
.username("")
.directory("bar")
.suffix("xml")
.query("")
.tld("com")
.query({ foo: "bar", hello: ["world", "mars"] });
URI("?&foo=bar&&foo=bar&foo=baz&")
.normalizeQuery();
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/world.html");
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/sub/world.html")
.absoluteTo("/foo/bar/sub/world.html");
URI.expand("/foo/{dir}/{file}", {
dir: "bar",
file: "world.html"
});
See the About Page and API Docs for more stuff.
npm
npm install URIjs
Server-side JS
var URI = require('URIjs');
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/sub/world.html")
Minify
See the build tool or use Google Closure Compiler:
// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name URI.min.js
// @code_url http://medialize.github.com/URI.js/src/IPv6.js
// @code_url http://medialize.github.com/URI.js/src/punycode.js
// @code_url http://medialize.github.com/URI.js/src/SecondLevelDomains.js
// @code_url http://medialize.github.com/URI.js/src/URI.js
// @code_url http://medialize.github.com/URI.js/src/URITemplate.js
// ==/ClosureCompiler==
Resources
Docs where you get more info on parsing and working with URLs
Discussion on Hacker News
HTML5 URL Draft
MozURLProperty
Alternatives
If you don't like URI.js, you may like one of these:
URL Manipulation
URL Parsers
URI Template
Authors
Contains Code From
License
URI.js is published under the MIT license and GPL v3.
Changelog
1.7.2 (August 28th 2012)
1.7.1 (August 14th 2012)
1.7.0 (August 11th 2012)
1.6.3 (June 24th 2012)
1.6.2 (June 23rd 2012)
1.6.1 (May 19th 2012)
1.6.0 (March 19th 2012)
1.5.0 (February 19th 2012)
- adding Second Level Domain (SLD) Support - (Issue #17)
1.4.3 (January 28th 2012)
1.4.2 (January 25th 2012)
1.4.1 (January 21st 2012)
1.4.0 (January 12th 2012)
1.3.1 (January 3rd 2011)
- updating Punycode.js to version 0.3.0
- adding edge-case tests ("jim")
- fixing edge-cases in .protocol(), .port(), .subdomain(), .domain(), .tld(), .filename()
- fixing parsing of hostname in
.hostname()
1.3.0 (December 30th 2011)
- adding
.subdomain()
convenience accessor - improving internal deferred build handling
- fixing thrown Error for
URI("http://example.org").query(true)
- (Issue #6) - adding examples for extending URI.js for fragment abuse, see src/URI.fragmentQuery.js and src/URI.fragmentURI.js - (Issue #2)
1.2.0 (December 29th 2011)
1.1.0 (December 28th 2011)
1.0.0 (December 27th 2011)