简体   繁体   中英

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, ie there is nothing after the base url.

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

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 (eg 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':
    // …
}
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM