Fork me on GitHub

URI.js

URI.js is a facility for working with URLs. It offers a "jQuery-style" API 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.

Examples

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

// mutating URLs
URI("http://example.org/foo.html?hello=world")
    .username("rodneyrehm") 
        // -> http://[email protected]/foo.html?hello=world
    .username("") 
        // -> http://example.org/foo.html?hello=world
    .directory("bar")
        // -> http://example.org/bar/foo.html?hello=world
    .suffix("xml")    
        // -> http://example.org/bar/foo.xml?hello=world
    .search("")
        // -> http://example.org/bar/foo.xml
    .tld("com")      
        // -> http://example.com/bar/foo.xml
    .search({ foo: "bar", hello: ["world", "mars"] });
        // -> http://example.com/bar/foo.xml?foo=bar&hello=world&hello=mars

How do you like working query strings?

URI("?hello=world")
    .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
    .removeSearch("foo")
        // -> ?hello=world
    

How do you like relative paths?

URI("/relative/path")
    .relativeTo("/relative/sub/foo/sub/file")
        // -> ../../../path 
    .absoluteTo("/relative/sub/foo/sub/file");
        // -> /relative/path 

How do you like cleaning things up?

URI("?&foo=bar&&foo=bar&foo=baz&")
    .normalizeSearch();
        // -> ?foo=bar&foo=baz

URI("/hello/foo/woo/.././../world.html")
    .normalizePathname();
        // -> /hello/world.html 

Authors

License

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