Javascript 当前页面是基本url吗?
我需要看看用户所在的当前页面是否是网站的主页,即在基本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
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':…}