jQuery url Parser

Overview

A jQuery plugin to parse urls and provides easy access to information within them, such as the protocol, host, port, the segments that make up the path and the various query string values.

Created and maintained by Mark Perkins. Visit http://projects.allmarkedup.com/jquery_url_parser/ for up-to-date releases, documentation and more.

The parser is based on the Regex URI parser by Steven Levithan - http://blog.stevenlevithan.com/archives/parseuri.

This version: 1.0
Dependancies: jQuery 1.2 or greater (may work with earlier releases but untested!)
Licence: Released under a MIT-style licence.

Usage Notes

By default, the parser will use the url of the current page. This can be changed to use a url passed in manually if required (see code example below).

There are two modes of url parser - strict and loose. Loose is the default parsing mode, it deviates from the specs slightly but splits the url up in a more intuitive manner. It is the recommended parsing mode. For more information on the two different parsing modes, see Steven Levithan's blog post (linked above) on the url parser used in the parser.

The parser can return the following information about the url:

The source, protocol, host, port, relative, path, directory, file, query and anchor strings are available through the .attr() method.

The query string parameters are available through the .param() method

The individual path segements are available through the .segment() method

Examples of Use

Using the current page's url (for these examples https://mysite.com/information/about/index.html?itemID=2&user=dave)

// get the protocol
jQuery.url.attr("protocol") // returns 'http'

// get the path
jQuery.url.attr("path") // returns '/information/about/index.html'

// get the host
jQuery.url.attr("host") // returns 'mysite.com'

// get the value for the itemID query parameter
jQuery.url.param("itemID") // returns 2

// get the second segment from the url path
jQuery.url.segment(2) // returns 'about'

Using a differnt url to the current page:

// get the protocol
jQuery.url.setUrl("http://allmarkedup.com/category/javascript/#footer").attr("anchor") // returns 'footer'