Skip to content

URL Properties

The location object provides properties to access every part of the current URL.

// For URL: http://example.com:8080/path/page?q=js#section
location.href; // 'http://example.com:8080/path/page?q=js#section'
location.protocol; // 'http:'
location.host; // 'example.com:8080'
location.hostname; // 'example.com'
location.port; // '8080'
location.pathname; // '/path/page'
location.search; // '?q=js'
location.hash; // '#section'
location.origin; // 'http://example.com:8080'
// Changing location properties navigates to the new URL
location.hash = '#new-section'; // Scrolls to #new-section without reload
location.search = '?q=new-search'; // Reloads with new query string
PropertyExampleDescription
hrefFull URLComplete URL string
protocolhttp:Protocol with colon
hostexample.com:8080Hostname + port
pathname/path/pagePath after host
search?q=jsQuery string
hash#sectionFragment identifier