Location object
Location object
The Location object contains information about the current URL.
The Location object is part of the window object and can be accessed via related properties in the format **window.location.**xxx.
Note: There is no public standard applied to the Location object, but all browsers support it.
Location Object Properties
| property | Description |
|---|---|
| hash | Returns the anchor part of a URL. |
| host | Returns the hostname and port of a URL. |
| hostname | Returns the hostname of a URL. |
| href | Returns the complete URL. |
| pathname | Returns the pathname of the URL. |
| port | Returns the port number used by the URL's server. |
| protocol | Returns the protocol of a URL. |
| search | Returns the query part of a URL. |
Example
// Create an tag and set its href attribute
var
url
=
document.
createElement
(
'a'
)
;
url.
href
=
'https://www.runoob.com/?s=python#test'
;
console.
log
(
url.
href
)
;
// https://www.runoob.com/?s=python
console.
log
(
url.
protocol
)
;
// https:
console.
log
(
url.
host
)
;
// www.runoob.com
console.
log
(
url.
hostname
)
;
// www.runoob.com
console.
log
(
url.
port
)
;
// (output is empty - the default port for https is 443)
console.
log
(
url.
pathname
)
;
// /
console.
log
(
url.
search
)
;
// en-US/search
console.
log
(
url.
hash
)
;
// #test
console.
log
(
url.
origin
)
;
// https://www.runoob.com
The test results are 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. |