Parsing a URL


This page was started whilst I was investigating how to extract information from the URL and the page itself for the breadcrumb navigation on the site. JavaScript has quite an array of functions available to do this.

The demonstrations on this page are simply...

<script type="text/javascript" language="javascript">

In a useful JavaScript function the information will probably need to be placed in a variable, this can be simply achieved by using something similar to...

var path = location.pathname;

This page does not give all of the location or document properties but gives the most useful for finding information about the current page.

Because not all possible parts of the URL are included in this page's URL some of the examples may appear empty.


URLs are not a single entity but are made up of several pieces of information. A URL can be made up of the following...


To return the entire URL the location.href property is used. For this page this is...


The protocol is the connection method. The most commonly used are HTTP, HTTPS, FTP and Mailto though there are others. Notice that location.protocol also returns the first colon...


The hostname property specifies the server name, subdomain and domain name (or IP address) of a URL. This is the string given by location.hostname...


The port property is a string specifying the communications port that the server uses. This is the string given by location.port...

Host & Port

These properties can be returned together using


The pathname property is a string portion of a URL specifying how a particular resource can be accessed. This is the string given by location.pathname...

Hash and Anchors

Sometimes a URL will contain a hash (#) or so that an anchor tag can be referenced. The string returned by location.hash begins with a #...


Sometimes a URL will contain information preceeded by a ? This can be extracted from a URL by using


This is a document property and can be extracted using document.title...

Sources and Information

This page created 3rd July 2005, last modified 3rd July 2005

GoStats stats counter