I am using this code to detect the homepage and it works great:
var url= window.location.href; if(url.split("/").length>3){ alert('You are in the homepage'); }
My problem is that I also need to detect if the url has variables for example:
mysite.com?variable=something
I need to also detect if the url has variables on it too
How can I do this?
Using window.location.pathname
could work too:
if ( window.location.pathname == '/' ){ // Index (home) page } else { // Other page console.log(window.location.pathname); }
See MDN info on window.location.pathname.
You can find out if you're on the homepage by comparing href to origin:
window.location.origin == window.location.href
To get the query parameters you can use the answer here: How can I get query string values in JavaScript?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With