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

IMPORTANT: You may not need URI.js anymore! Modern browsers provide the URL and URLSearchParams interfaces.

NOTE: The npm package name changed to urijs

var url = "";
var separator = url.indexOf('?') > -1 ? '&' : '?';

url += separator + encodeURIComponent("foo") + "=" + encodeURIComponent("bar");

Things are looking up with URL and the URL spec but until we can safely rely on that API, have a look at URI.js for a clean and simple API for mutating URIs:

var url = new URI("");
url.addQuery("foo", "bar");

URI.js is here to help with that.

API Example

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

// cleaning things up
    // -> ?foo=bar&foo=baz

// working with relative paths
    // -> ./baz.html

    // -> ../baz.html
    // -> /foo/bar/baz.html

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

See the About Page and API Docs for more stuff.

Using URI.js

URI.js (without plugins) has a gzipped weight of about 7KB – if you include all extensions you end up at about 13KB. So unless you need second-level domain support and use URI templates, we suggest you don’t include them in your build. If you don’t need a full-featured URI mangler, it may be worth looking into the much smaller parser-only alternatives listed below.

URI.js is available through npmbowerbowercdncdnjs, and manually from the build page:

# using bower
bower install uri.js

# using npm
npm install urijs