JQuery Detect If in Homepage and Homepage PLUS url Variables

Satch3000 picture Satch3000 · Jun 24, 2013 · Viewed 41.8k times · Source

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?

Answer

Bradley Flood picture Bradley Flood · Apr 22, 2015

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.