JavaScript Window Location
Location Object
The JavaScript Location object is used to get or set the current URL information. The Location object is part of the window object and can be accessed through the window.location property.
The Location object is often used to obtain the information in the URL address, or refresh the current page, page redirection, etc. For details, see the properties and methods listed below.
Location Object Properties
PropertiesDescription
location.hash Set or get the anchor in the URL
location.host Set or get the host (including port number) in the URL
location.hostname Set or get the host name in the URL
location.href Set or get the complete URL (page redirection application)
location.pathname Set or get the path in the URL
location.port Set or get the URL Port number
location.protocol Set or get the protocol used by the URL
location.search Set or get the query string in the URL (usually the content after the ? symbol)
Location object methods
The History object has the following three methods:
location.assign(): Load a new page document
location.reload (): Reload (refresh) the current page
location.replace(): Replace the current document with the new document
JavaScript location.href attribute
The href attribute of the Location object is used to set or get the current complete URL. The syntax is as follows:
location.href = URL
#location.href attribute is most commonly used for page jumps (redirects) in JavaScript
Returns the entire URL (of the current page):
php中文网(php.cn)
JavaScript location.pathname attribute
The pathname attribute of the Location object is used to set or get the path part of the current URL. The syntax is as follows:
location.pathname = path
Return the path name of the current URL:
php中文网(php.cn)
JavaScript location.assign() method
The assign() method of the Location object is used to load a new document. The syntax is as follows:
location.assign(URL)
Load a new document:
php中文网(php.cn)