简体   繁体   English

当前页面是基本URL吗?

[英]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. 阅读(跨浏览器) window.location对象的更多信息。

Edit : If your 'root' maybe served by some file named index (eg index.html , index.php , index.asp , etc.) you may want: 编辑 :如果您的“root”可能由名为index文件(例如index.htmlindex.phpindex.asp等)提供,您可能需要:

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 更多信息: http//www.w3schools.com/jsref/prop_loc_pathname.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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