DEV Community

Cover image for window.location Cheatsheet

window.location Cheatsheet

Samantha Ming on April 22, 2020

Looking for a site's URL information, then the window.location object is for you! Use its properties to get information on the current page addre...
Collapse
 
mirkan1 profile image
Mirkan

Great article but I found an error

on

Here's the complete list of properties that you can change:

// Example

window.location.protocol = 'https'
               .host     = 'localhost'
               .hostname = 'localhost:8080'
               .port     = '8080'
               .pathname = 'path'
               .search   = 'query string' // (you don't need to pass ?)
               .hash     = 'hash' // (you don't need to pass #)
               .href     = 'url'

.hostname and .host should be swaped

take care, Renas

Collapse
 
samanthaming profile image
Samantha Ming

AH yikes 😱 Let me fix that now!! Great catch! Thanks for letting me know 😰

Collapse
 
mirkan1 profile image
Mirkan

Can you follow me back if posible?

Collapse
 
corentinbettiol profile image
Corentin Bettiol

Good to know. I think I'm starting to understand how dev.to work and why it is so fast (hover on link => make request and store result in cache, click on link = assign url and load cached content).

I may implement those tricks to enhance user experience on my websites :)

Collapse
 
samanthaming profile image
Samantha Ming

woooo, would love to see the inner working of how dev.to works 🤩

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

It's Preact. You can find source code on GitHub.

Collapse
 
andy profile image
Andy Zhao (he/him)

We use InstantClick for that "hover on link => make a request" effect :) instantclick.io/

Loading cached content is mostly because of Fastly!

Most of the pages are Rails pages, and there's some Preact mixed in every once in a while.

Collapse
 
corentinbettiol profile image
Corentin Bettiol

Don't really know how caching is managed or anything about the Rail framework, but instanclick seems to be a gold nugget!

Thanks for the link, and bravo for this insanely fast website :)

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...

Collapse
 
ben profile image
Ben Halpern

This is fabulous

Collapse
 
samanthaming profile image
Samantha Ming

Thanks Ben! 😄

Collapse
 
5t3ph profile image
Stephanie Eckles

When I was trying to decide an efficient way to determine if files were being served on localhost or prod, I came here and ended up with if(window.location.port) to test for localhost 🙌I'm sure I will visit more in the future, great job creating an excellent, clear resource!

Collapse
 
samanthaming profile image
Samantha Ming

Nice! Glad you found the article helping and you were able to apply the knowledge right away! I also list all my tidbits on my site if you want another resource to check out 😊 > samanthaming.com/

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I often use new URL(location.href).searchParams, BTW. Much more powerful than location.search.

Collapse
 
samanthaming profile image
Samantha Ming

OH COOL! must be a new-ish web API, no support for IE ... hmm...did they create this to hopefully replace window.location 🤔

I'll need to look into it, maybe for a future tidbit 😊

Collapse
 
mirzaa profile image
Maas Mirzaa

Wonderful, thanks for the breakdown :)

Collapse
 
samanthaming profile image
Samantha Ming

You're very welcome! Thanks for reading 😄

Collapse
 
victorekpodecodedigital profile image
victorekpo-decodedigital

Awesome!

Collapse
 
samanthaming profile image
Samantha Ming

Thank you! 🙂

Collapse
 
chiubaca profile image
Alex Chiu

brilliant break down! really filled in the gaps for me. 🙌

Collapse
 
samanthaming profile image
Samantha Ming

awesome, glad to hear that! Thanks for reading my article 😊

Collapse
 
mustapha profile image
Mustapha Aouas

This is a great Post!
I learned something thanks 💯

Collapse
 
samanthaming profile image
Samantha Ming

Thanks Mustapha! So glad this post was helpful for you 👏

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

You're so good at explaining this stuff to other developers, Samantha! I learned a bunch of things from reading.

Collapse
 
samanthaming profile image
Samantha Ming

Thank you Eric! Being able to explain and communicate where other people can understand is a skill I'm trying to get better at. So I'm glad to hear I'm doing okay 😆 -- there's still more room for improvement! I'll keep practicing -- hopefully one day i can explain it where non-dev will also understand it and they will be encouraged to learn programming 😊

Collapse
 
dotorimook profile image
dotorimook

Thanks for this well-organized post!

Collapse
 
lucianobarauna profile image
Luciano Baraúna

Nice guide for a consultation

Collapse
 
andreisena profile image
Andrei Sena

Hi! Great article!

On the first example (samanthaming.com/tidbits/?filter=JS#2), window.location.href would return the entire URL.

Collapse
 
samanthaming profile image
Samantha Ming

ah yikes! great catch, let me fix it 😱 Thanks for letting me know 😨

Collapse
 
jesuissuyaa profile image
🐟

Thank you so much for taking your time and effort to write this article AND adding a beautiful infographic ♥️

Collapse
 
samanthaming profile image
Samantha Ming

Glad you enjoyed the image and article! that's a double success, thanks for the positive feedback 😊