繁体   English   中英

当前页面是基本URL吗?

[英]Is the current page the base url?

我需要查看用户当前页面是否是网站的主页面,即基本网址后面没有任何内容。

我这样做是为了从主页面中排除一些代码。

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

阅读(跨浏览器) window.location对象的更多信息。

编辑 :如果您的“root”可能由名为index文件(例如index.htmlindex.phpindex.asp等)提供,您可能需要:

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

或更明确地:

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
}

更多信息: http//www.w3schools.com/jsref/prop_loc_pathname.asp

暂无
暂无

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

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