Fork me on GitHub

URI.js

Understanding URIs

Uniform Resource Identifiers (URI) can be one of two things, a Uniform Resource Locator (URL) or a Uniform Resource Name (URN). You likely deal with URLs most of the time. See RFC 3986 for a proper definition of the terms URI, URL and URN

URLs are used to address the individual resources of your website. URNs are usually used for hooking into other applications, as mailto:, magnet: or spotify: suggest. While RFC 3986 defines the structure of an URL in depth, URNs are not. The structure (and meaning) of URNs are up to their distinct specifications.

Components of an URI

RFC 3986 Section 3 visualizes the structure of URIs as follows:

URL:      foo://example.com:8042/over/there?name=ferret#nose
          \_/   \______________/\_________/ \_________/ \__/
           |           |            |            |        |
        scheme     authority       path        query   fragment
           |   _____________________|__
          / \ /                        \
URN:      urn:example:animal:ferret:nose

Components of an URL in URI.js

                         authority
                   __________|_________
                  /                    \
              userinfo                host
               __|___                ___|___
              /      \              /       \
         username  password     hostname    port     path & segment      query   fragment
           __|___   __|__    ______|______   |   __________|_________   ____|____   |
          /      \ /     \  /             \ / \ /                    \ /         \ / \
    foo://username:[email protected]:123/hello/world/there.html?name=ferret#foo
    \_/                     \ / \       \ /    \__________/ \     \__/
     |                       |   \       |           |       \      |
  scheme               subdomain  \     tld      directory    \   suffix
                                   \____/                      \___/
                                      |                          |
                                    domain                   filename

In Javascript the query is often referred to as the search. URI.js provides both accessors with the subtle difference of .search() beginning with the ?-character and .query() not.

In Javascript the fragment is often referred to as the hash. URI.js provides both accessors with the subtle difference of .hash() beginning with the #-character and .fragment() not.

Components of an URN in URI.js

    urn:example:animal:ferret:nose?name=ferret#foo
    \ / \________________________/ \_________/ \ /
     |               |                  |       |
  scheme       path & segment         query   fragment
    

While RFC 3986 does not define URNs having a query or fragment component, URI.js enables these accessors for convenience.