Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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_Jquery - Fatal编程技术网

Javascript 检查用户是否正在访问纯根url

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

如果用户正在访问我的站点的根url,我希望运行js脚本。我正在使用以下代码:

<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)}
我需要你的相反的情况,所以你的代码没有修复就是我想要的,谢谢:)