JavaScript Internals (521 Qs)•Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)
What are the various url properties of location object
The below Location object properties can be used to access URL components of the page,
- href - The entire URL
- protocol - The protocol of the URL
- host - The hostname and port of the URL
- hostname - The hostname of the URL
- port - The port number in the URL
- pathname - The path name of the URL
- search - The query portion of the URL
- hash - The anchor portion of the URL
Practical Code Example
javascript
// Given URL: https://example.com:8080/dashboard?user=123#analytics
console.log(window.location.protocol); // 'https:'
console.log(window.location.host); // 'example.com:8080'
console.log(window.location.hostname); // 'example.com'
console.log(window.location.port); // '8080'
console.log(window.location.pathname); // '/dashboard'
console.log(window.location.search); // '?user=123'
console.log(window.location.hash); // '#analytics'