Fork me on GitHub
URI.js Github Repository


URI.js is a javascript library for working with URLs. It offers a "jQuery-style" API (Fluent Interface, Method Chaining) to read and write all regular components and a number of convenience methods like .directory() and .authority().

URI.js offers simple, yet powerful ways of working with query string, has a number of URI-normalization functions and converts relative/absolute paths.

While URI.js provides a jQuery plugin. URI.js itself does not rely on jQuery. You don't need jQuery to use URI.js


How do you like manipulating URLs the "jQuery-style"?

// mutating URLs
        // ->
        // ->
        // ->
        // ->
        // ->
        // ->
    .search("") // alias of .query()
        // ->
        // ->
    .search({ foo: "bar", hello: ["world", "mars"] });
        // ->

How do you like working query strings?

    .addSearch("hello", "mars")
        // -> ?hello=world&hello=mars
    .addSearch({ foo: ["bar", "baz"] })
        // -> ?hello=world&hello=mars&foo=bar&foo=baz
    .removeSearch("hello", "mars")
        // -> ?hello=world&foo=bar&foo=baz
        // -> ?hello=world

How do you like relative paths?

        // -> ../../../path 
        // -> /relative/path 

How do you like cleaning things up?

        // -> ?foo=bar&foo=baz

        // -> /hello/world.html 

How do you like detecting URIs within random text?

var source = "Hello,\n"
    + " is a search engine, like\n"
    + "http://exä is an IDN URL,\n"
    + " is IPv4 and "
    + "http://fe80:0000:0000:0000:0204:61ff:fe9d:f156/foobar.html is IPv6.\n"
    + "links can also be in parens ( "
    + "or quotes »«.";

var result = URI.withinString(source, function(url) {
    return "<a>" + url + "</a>";

/* result is:
Hello <a></a>,
<a></a> is a search engine, like <a></a>
<a>http://exä</a> is an IDN URL,
<a></a> is IPv4 and <a>http://fe80:0000:0000:0000:0204:61ff:fe9d:f156/foobar.html</a> is IPv6.
links can also be in parens (<a></a>) or quotes »<a></a>«.

How do you like comparing URLs?

var a = ""
        + "?foo=bar&hello=world&hello=mars#fragment",
    b = ""
        + "?hello=mars&foo=bar&hello=world&#fragment";

a !== b;
URI(a).equals(b) === true;

How do you like fiddling with the fragment?

// storing values in the fragment (hash):
var uri = URI("#hello-world");
uri.fragment({foo: "bar", bar: ["hello", "world"]});
uri.fragment() === "foo=bar&bar=hello&bar=world";
// required src/URI.fragmentQuery.js to be loaded

// storing URLs in the fragment (hash):
var uri = URI("!/foo/bar/baz.html"),
    furi = uri.fragment(true);
furi.pathname() === "/foo/bar/baz.html";
uri.toString() === "!/hello.html"
// required src/URI.fragmentURI.js to be loaded

How do you like parsing URNs?

var uri = URI("urn:uuid:c5542ab6-3d96-403e-8e6b-b8bb52f48d9a?query=string");
uri.protocol() == "urn";
uri.path() == "uuid:c5542ab6-3d96-403e-8e6b-b8bb52f48d9a";
uri.query() == "query=string";

How do you like URI Templating?

URI.expand("/foo/{dir}/{file}", {
  dir: "bar",
  file: "world.html"
// -> /foo/bar/world.html



URI.js is published under the MIT license and GPL v3.