Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当前页面是基本url吗?_Javascript - Fatal编程技术网

Javascript 当前页面是基本url吗?

Javascript 当前页面是基本url吗?,javascript,Javascript,我需要看看用户所在的当前页面是否是网站的主页,即在基本url之后没有任何内容 我这样做是为了从主页上排除一些代码 if (location.pathname=="/"){ // at the root of the site } 阅读有关(跨浏览器)对象的详细信息 编辑:如果您的“根目录”可能由名为索引的文件提供(例如index.html,index.php,index.asp等),您可能需要: var rootPattern = /^\/(?:index\.[^.\/]+)?$/i; i

我需要看看用户所在的当前页面是否是网站的主页,即在基本url之后没有任何内容

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

if (location.pathname=="/"){
  // at the root of the site
}
阅读有关(跨浏览器)对象的详细信息

编辑:如果您的“根目录”可能由名为
索引
的文件提供(例如
index.html
index.php
index.asp
等),您可能需要:

var rootPattern = /^\/(?:index\.[^.\/]+)?$/i;
if (rootPattern.test( location.pathname )){
  // …
}
或者更明确地说:

switch(location.pathname){
  case '/':
  case '/index.html':
  case '/index.php':
  case '/index.asp':
    // …
}
阅读有关(跨浏览器)对象的详细信息

编辑:如果您的“根目录”可能由名为
索引
的文件提供(例如
index.html
index.php
index.asp
等),您可能需要:

var rootPattern = /^\/(?:index\.[^.\/]+)?$/i;
if (rootPattern.test( location.pathname )){
  // …
}
或者更明确地说:

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


更多信息:

请注意,尽管在JS中可以做到这一点,但如果您想阻止用户运行JS的某些部分,这并不一定是一个好主意。JS在客户端上运行,因此用户可以完全控制它,并可以根据自己的意愿进行更改。请注意,虽然在JS中可以这样做,但如果您想阻止用户运行JS的某些部分,这并不一定是一个好主意。JS在客户端上运行,因此用户可以完全控制它,并可以根据自己的意愿对其进行更改。或者更简洁地说:
switch(location.pathname){case'/':case'/index.html':case'/index.php':…}
或者更简洁地说:
switch(location.pathname){case'/':case'/index.html':…}