Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Detect If in Homepage and Homepage PLUS url Variables

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?

like image 297
Satch3000 Avatar asked Jun 24 '13 08:06

Satch3000


2 Answers

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.

like image 97
Bradley Flood Avatar answered Sep 20 '22 06:09

Bradley Flood


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?

like image 38
Mataniko Avatar answered Sep 20 '22 06:09

Mataniko