【问题标题】:Check if there is sub-url in Jquery?检查Jquery中是否有子网址?
【发布时间】:2014-05-06 06:45:31
【问题描述】:

我需要检查url中是否有子页面?

类似的东西

www.example.com

alert ('This is root of website');

www.example.com/about.html

alert('This is not root of website');

只有当用户是网站的root,主页时如何提醒?

【问题讨论】:

    标签: jquery indexof window.location


    【解决方案1】:

    您可以使用window.location.pathname获取当前URL的路径名:

    if(window.location.pathname.length > 1) {
        alert('This is not root of website');
    } else {
        alert ('This is root of website');
    }
    

    【讨论】:

    • 没有错误,但我不需要别的,我只需要在 root 时提醒
    【解决方案2】:
    var url = "www.example.com/about.html";
    
    if(url.split("/")[1] != ""){
    
      // sub url present
    }
    
    else{
    
    // not present
    }
    

    【讨论】:

      【解决方案3】:
      if(location.pathname.length>1){
      alert('This is not root of website');
      }
      else{
      alert ('This is root of website');
      }
      

      【讨论】:

        【解决方案4】:

        试试location 全局对象。使用它的pathname 属性:

        alert( location.pathname );
        

        所以使用类似下面的东西

        if (location.pathname == "/") {
            alert("Homepage");
        } else {
            alert("not homepage");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-12
          • 1970-01-01
          • 2013-07-17
          • 1970-01-01
          • 2013-01-05
          • 2021-06-20
          • 2012-09-05
          • 2011-09-25
          相关资源
          最近更新 更多