DEV Community

Discussion on: window.location Cheatsheet

Collapse
 
myfonj profile image
Michal Čaplygin • Edited

Heads up: root URLs always end with slash. More specifically, the path part that follows host and port is mandatory and according URL format specification cannot be blank nor omitted and must start with slash, so the shortest value usually denoting root path is just '/'. Most agents tend to fix invalid input and silently add missing parts of URL before making request, and some user agents tend to hide single slash of root paths for (dubiously) aesthetic reasons, but actual HTTP request such agent produces always involves / path. "URLs" like http://host, http://host?search, http://host#hash are all in fact invalid and should be expressed as http://host/, http://host/?search, http://host/#hash.

window.location.href;
// never yields 'https://www.samanthaming.com'
// but rather 'https://www.samanthaming.com/'

url.spec.whatwg.org/#concept-url-path
url.spec.whatwg.org/#path-absolute...