JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Window Location

The location property of a window (i.e. window.location) is a reference to a Location object.

The Location Object

The window.location object represents the current URL of the document being displayed in a window.

The window.location object can be written without the window prefix.

Some examples:

The following section will show you how to get the URL of page as well as hostname, protocol, etc. using the location object property.

Getting the Current Page URL

The location.href property returns the URL of the current page.

Run code

Getting the Hostname

The location.hostname property returns the name of the internet host (of the current page).

Run code

Getting the Current Page Pathname

The location.pathname property returns the pathname of the current page.

A pathname is a string that starts after hostname containing an initial '/' followed by the path of the URL.

Run code

Getting the Current Page Protocol

The location.protocol property returns the web protocol of the current URL, including the colon (:).

Run code

Getting the Port Number

The location.port property returns the number of the internet host port (of the current page).

Run code

Note: The port property returns an empty string if the port number is not specified in the URL or if it is the scheme's default port (80 for http and 443 for https).

Window Location Assign

The location.assign() method causes the window to load and display the document at the URL specified.

Run code

Complete Location Reference

For a complete properties and methods reference, visit our JavaScript Location Object Reference.

The reference section contains descriptions and examples of all location properties and methods.