If you want to get the URL information of the site, then the window.location
object is suitable for you! Use its properties to get information about the current page address, or use its methods to do some page redirection or refresh
window.location.origin → '"https://segmentfault.com' .protocol → 'https:' .host → 'segmentfault.com' .hostname → 'segmentfault.com' .port → '' .pathname → '/search' .search → '?q=前端小智' .hash → '#2' .href → 'https://segmentfault.com/search?q=前端小智#2'
window.location.assign('url') .replace('url') .reload() .toString()
window.location | Return value |
---|---|
.origin | Site main address (protocol host name port ) |
.protocol | Protocol architecture (http: or htts: ) |
.host | Domain name port |
.port | Port |
.pathname | The path followed by '/' on the first page |
.search | ? The query string followed |
.hash | Part starting from
|
#.href | Full URL |
In the above example, you may notice that host
and hostname
Return the same value. So why these properties. Well, it's about the port number, so let's take a look.
URL without port
https://segmentfault.com/search
window.location.host; // 'segmentfault.com' window.location.hostname; // 'segmentfault.com' window.location.port; // ''
URL with port
##https://segmentfault.com/search"8080Therefore,window.location.host; // 'segmentfault.com:8080' window.location.hostname; // 'segmentfault.com' window.location.port; // '8080'Copy after login
host will include the port number, while
hostname will return only the host name.
// 开始 'https://segmentfault.com/' window.location.pathname = '/tidbits'; // 设置 pathname // 结果 'https://segmentfault.com/tidbits'
// 事例 window.location.protocol = 'https' .host = 'localhost:8080' .hostname = 'localhost' .port = '8080' .pathname = 'path' .search = 'query string' // (这里不用写 `?`) .hash = 'hash' // (这里不用写 `#`) .href = 'url'
window.location.origin, which is read-only.
window.location Returns a
Location object. It gives us information about the current address of the page. But we can also access the
Location object in several ways.
window.location → Location window.document.location → Location document.location → Location location → Location
Location object. I personally prefer
window.location and would actually avoid using
location. Mainly because
location looks like a normal variable, and we may sometimes accidentally name it as a variable, which will override the global variable. For example:
// https://www.samanthaming.com location.protocol; // 'https' function localFile() { const location = '/sam'; return location.protocol; // undefined // b/c local "location" has override the global variable }
window is a global variable. That way it's less likely to cause confusion. To be honest, I didn't know
location## until I wrote this article # is a global variable. It is recommended that you use window.location
instead of other writing methods.
Function | |
---|---|
.replace() | |
.reload() | |
.reload() | |
The above is the detailed content of Detailed explanation of window.location object in JS (cheat sheet). For more information, please follow other related articles on the PHP Chinese website!