LocationObject


Location Object

The Location object contains information about the current URL.

The Location object is part of the window object and can be accessed viawindow.location.xxxthe relevant properties of the format to access it.

NoteNote:There is no public standard that applies to the Location object, but all browsers support it.


Location Object Properties

Property Description
hash Returns the anchor portion of a URL
host Returns the hostname and port of a URL
hostname Returns the hostname of a URL
href Returns the full URL
pathname Returns the URL pathname.
port Returns the port number used by a URL's server
protocol Returns the protocol of a URL
search Returns the query portion of a URL

Examples

// Create an a tag and set the href attribute
var url = document.createElement('a');
url.href = 'https://www.example.com/?s=python#test';
console.log(url.href);      // https://www.example.com/?s=python
console.log(url.protocol);  // https:
console.log(url.host);      // www.example.com
console.log(url.hostname);  // www.example.com
console.log(url.port);      // (Output is empty - https default port is 443)
console.log(url.pathname);  // /
console.log(url.search);    // en-US/search
console.log(url.hash);      // #test
console.log(url.origin);    // https://www.example.com

The test result is as follows:

Location Object Methods

Method Description
assign() Loads a new document
reload() Reloads the current document
replace() Replaces the current document with a new document
Other Extensions