InterviewHubPRO
Section Overview
Q128

Question 128 of 521

Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

25%
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,

  1. href - The entire URL
  2. protocol - The protocol of the URL
  3. host - The hostname and port of the URL
  4. hostname - The hostname of the URL
  5. port - The port number in the URL
  6. pathname - The path name of the URL
  7. search - The query portion of the URL
  8. 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'