Javascript 检查用户是否正在访问纯根url
如果用户正在访问我的站点的根url,我希望运行js脚本。我正在使用以下代码:Javascript 检查用户是否正在访问纯根url,javascript,jquery,Javascript,Jquery,如果用户正在访问我的站点的根url,我希望运行js脚本。我正在使用以下代码: <script type="text/javascript"> if(location.pathname == "/") { window.open ('#107','_self',false); } </script> 我希望它的行为如下: http://example/ Root /?querys
<script type="text/javascript">
if(location.pathname == "/") {
window.open ('#107','_self',false);
}
</script>
我希望它的行为如下:
http://example/ Root
/?querystring Root
/#hash Root
/page Not root
http://example/ Root
/?querystring Not root
/#hash Not root
/page Not root
有什么建议吗?
谢谢。改用
window.location.href
您可以连接路径名、location.search和location.hash
var fullPath = location.pathname + location.search + location.hash;
if(fullPath == "/") { /* whatever */ }
您只需测试所有三个组件:
<script type="text/javascript">
if(location.pathname == "/" &&
location.hash.length <= 1 &&
location.search.length <= 1) {
window.open ('#107','_self',false);
}
</script>
如果(location.pathname==“/”&&
location.hash.length我将我的代码编辑为以下内容,它起了作用:if(window.location.href==)http://example.com/“{window.open('#107',u self',false)}
我需要你的相反的情况,所以你的代码没有修复就是我想要的,谢谢:)