【问题标题】:How to check if the user is visiting the site's root url?如何检查用户是否正在访问站点的根 URL?
【发布时间】:2011-12-27 09:10:34
【问题描述】:

如果用户正在访问我网站的根 URL,我想启动一些脚本。

例如,当用户访问时,我想在我的视图中做一些事情

  • www.example.com
  • example.com
  • www.example.com/
  • http://www.example.com
  • http://example.com

...[以上的各种组合]

而不是www.example.com/anything else
在 ASP.NET MVC 3 [Razor] 网站和 javascript 的视图页面中检查这一点的最安全方法是什么?另外,有什么方法可以只使用 javascript 找出来吗?
谢谢。

【问题讨论】:

    标签: javascript asp.net asp.net-mvc-3 razor


    【解决方案1】:

    最简单的 JavaScript 方法是:

    var is_root = location.pathname == "/"; //Equals true if we're at the root
    

    即使http://example.com/?foo=bar#hash 也会产生正确的结果,因为路径名不包括查询字符串和位置哈希。

    看看:

    http://anything-but-a-slash/                  Root
                               /?querystring      Root
                               /#hash             Root
                               /page              Not root
    

    如果您的根文件夹中有索引文件,请查看以下示例:

    var is_root =/^\/(?:|index\.aspx?)$/i.test(location.pathname);
    

    上一行使用的是正则表达式。特殊字符必须转义,/i 后缀使模式不区分大小写。如果您希望大小写匹配,请省略 i 标志。

    以图形方式呈现的相同正则表达式:

    【讨论】:

    • 感谢您的详尽回答。
    • 如果您还需要支持 localhost 试试这个,其中 /Site/ 代表项目 var is_root = (window.location.pathname == "/" || window.location.pathname == " /地点/”) ?真:假;
    • @afreeland 对于http://localhost/Site/location.pathname 显示/Site/。如果要临时测试一个网站,最好使用.htaccess重写root。请参阅:pastebin.com/4ZJzKJkE。另外,a == b 已经返回一个布尔值,不需要使用(a == b ? true : false)
    • 正则表达式将匹配任何以/ 开头的网页作为根页面。请检查此答案:stackoverflow.com/a/19473369/363573
    • @Alex 感谢您提出这个问题。我已经修改了正则表达式。
    【解决方案2】:

    试试这个更健壮的正则表达式:

    正则表达式

    var isRoot =/^(\/|\/index\.asp|\/index\.aspx)$/i.test(location.pathname);
    


    说明


    演示

    Debuggex Demo

    【讨论】:

      【解决方案3】:

      当我从在线 IDE 以调试/预览模式运行我的应用程序时,测试“/”的路径名失败。换句话说,它通常使用一个干净的 url(没有 .html)提供,除非我从 Cloud9 提供它,在这种情况下它使用 index.html

      所以,这对我有用:

      if(window.location.pathname.length == 1 || window.location.pathname.length == 0 || window.location.pathname === "/index.html" || window.location.pathname === "/index"){ //I'm at the app root }

      【讨论】:

        【解决方案4】:

        使用正则表达式:

        (/^https?\:\/\/[^\/]+\/?$/).test(window.location.href)

        【讨论】:

        • 似乎更容易使用window.location.pathname
        • @Mic 对正则表达式有点困惑,你能简单介绍一下吗?
        • 以 http http 开始 ^,可能是 https s? 然后是://,然后是除 / [^\/]+ 以外的任何内容,以 / 或不是 \/? 结尾,结束字符串$
        【解决方案5】:

        我觉得

        if(window.location == window.location.hostname) {
            //is root
        }
        

        编辑:不要使用这个,看评论

        【讨论】:

        • 谢谢。但window.location.hostname 不尊重端口号。所以在 dev 中,如果你在 80 以外的端口号上,你会得到错误的结果。
        【解决方案6】:

        下面的呢:

        if (location.pathname == "/" || (window.location.href.indexOf("Index") > -1)){
        
          console.log("do stuff") 
        
        }
        

        注意“索引”区分大小写

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-23
          • 2012-05-04
          • 2011-07-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多