Ajax JavaScript CSS HTML 5 Firefox

Windows location in JavaScript

The location interface allows to know the elements of the document's URL in the current window: the domain name, the file name, etc.

Location is an element of Window. This is a side-client (created by the browser) object and the interface is implemented in JavaScript since version 1.0.

In HTML 5, location is an interface integrated to DOM and also an attribute of HTMLDocument.

A URL is made up of a domain, a file name ...

Take the current page as an example of URLs to which we add all the possible parameters:


It is splitted as follows:


Which, translated into attributes of the location object is:


The properties of location in the previous example have these values:

protocol http:
host [www.xul.fr]:80
hostname www.xul.fr
pathname /javascript/window-location.php
port 80
hash #content
search ?name=value

In addition, href corresponds to the full URL.

Location assigns these elements to attributs

Properties of location

Contains the protocol of the page, including the colon.


Or ftp: etc.


Port number, which is optional. The colon is a separator and is not part of the value of the property.


String composed of all sub-domains possible, the domain with its extension. Example:


Combination of hostname + port. Example:


Directory and filename of the page. Example:


The hash mark # introduces an address inside a page. Example:

in /ecmascript/window-location.php#content, the anchor is content.

List of variables and their values. The code? is included in the value of the property. Example:


It will be easy with the demonstration script to try all possible combinations and see values returned.

It has methods to change the URL of the page

Methods of location

The method allows to change the location to load a new page. Example:


Reload the same page that is currently displayed.


Replaces the displayed page by another. The history becomes that of the replacing page.


The difference between assign and replace is that after replace the back button is disabled.

Example of using location to change the components of the URL

The location properties can be read or assigned.
For example the path of the page may be displayed:


And the page is changed the way:

location.href = "url"

The reload method allows also to reload the page according to the new URL.

The demo allows you to enter a URL in a form, displays the properties and execute the methods of the object and view properties so changed.

var url = document.loc.url.value;
var storage = document.getElementById("storage");

var href = "href  " + location.href 
var protocol = "protocol " + location.protocol 
var hostname = "";//"hostname " + location.hostname 
var port = "port " + location.port 
var host = "host " + location.host 
var pathname = "pathname " + location.pathname 
var hash = "hash " + location.hash 
var search = "search " + location.search 

var data = href + protocol + hostname + port + host + pathname + hash + search;
storage.innerHTML = data;

To use the demo, we change the parameters of the URL that must remain that of the demo page if you want to view these parameters with the "properties" button.
The other buttons call the methods of their label.

Demonstration of the value of properties of the Location interface in JavaScript, and its methods.

Enter a modified URL and click on Properties to display them in the page, on on other buttons to execute a method.
The page must be this demo to have a Properties button or any page you have uploaded that contains a script to display the properties, as the script in this page does.



Full code of the demo:

function properties()
  var url = document.loc.url.value;
  var storage = document.getElementById("storage");
  var href = "href  " + location.href ;
  var protocol = "protocol " + location.protocol;
  var hostname = "";//"hostname " + location.hostname;
  var port = "port " + location.port;
  var host = "host " + location.host;
  var pathname = "pathname " + location.pathname;	
  var hash = "hash " + location.hash;
  var search = "search " + location.search;
  var data = href + protocol + hostname + port + host + pathname + hash + search;
  storage.innerHTML = data;

function replace()
  var url = document.loc.url.value;

function reload()

function assign()
  var url = document.loc.url.value;

See also

© 2008-2012 Xul.fr