Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get anything following the domain in a url, using Javascript

What is the best way to get the "anything" part after the domain part, using Javascript:

http://www.domain.com/anything
http://www.domain.com/#anything
http://www.domain.com/any/thing

For http://www.domain.com/#anything I would have to use window.location.hash. But for http://www.domain.com/anything I would have to use window.location.pathname.

I'm using:

window.location.href.replace(window.location.origin, "").slice(1)

Are there any caveats with this solution? Is there a better way?

like image 478
Alex Vang Avatar asked Nov 01 '25 16:11

Alex Vang


1 Answers

window.location.pathname + window.location.search + window.location.hash

I think this one is a little bit better. You dont have to use any functions here...

like image 118
Entity Black Avatar answered Nov 04 '25 06:11

Entity Black



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!