Parampal Pooni
Parampal Pooni

Reputation: 3108

Is the current page the base url?

I need to see if the current page a user is on is the main page of the website, i.e. there is nothing after the base url.

I'm doing this to exclude some code off the main page.

Upvotes: 1

Views: 276

Answers (2)

Phrogz
Phrogz

Reputation: 303361

if (location.pathname=="/"){
  // at the root of the site
}

Read more on the (cross-browser) window.location object.

Edit: If your 'root' maybe served by some file named index (e.g. index.html, index.php, index.asp, etc.) you may want:

var rootPattern = /^\/(?:index\.[^.\/]+)?$/i;
if (rootPattern.test( location.pathname )){
  // …
}

or more explicitly:

switch(location.pathname){
  case '/':
  case '/index.html':
  case '/index.php':
  case '/index.asp':
    // …
}

Upvotes: 4

Andy Fleming
Andy Fleming

Reputation: 7865

if (location.pathname == "/" || location.pathname == "/index.html" || location.pathname == "/index.php"){
  // at the "main page" of the site
}

More here: http://www.w3schools.com/jsref/prop_loc_pathname.asp

Upvotes: 0

Related Questions