window.location object
In JavaScript, the window.location
object has several properties that you can use to get information about the current URL or manipulate it. Here are some key properties and methods:
window.location.hash: Returns the anchor part of a URL, including the hash sign (
#
).window.location.href: Returns the entire URL of the current page.
window.location.protocol: Returns the protocol scheme of the URL, including the final colon (
:
).window.location.host: Returns the host (hostname and port) of the URL.
window.location.hostname: Returns the domain name of the web host.
window.location.port: Returns the port number of the URL.
window.location.pathname: Returns the path of the URL.
window.location.search: Returns the query string of the URL, including the question mark (
?
).
Methods
window.location.assign(url): Loads the resource at the URL provided.
window.location.replace(url): Replaces the current document with the one at the URL provided. This method does not create a new entry in the browser's history.
window.location.reload(forceReload): Reloads the current URL. If
forceReload
is true, the page will be reloaded from the server.
These properties and methods are useful for manipulating the URL or redirecting the user to a different page.
Last updated