首页 > 技术文章 > 获取页面地址url的指定部分信息

wmqiang 2019-03-16 19:26 原文

获取页面地址url的指定部分信息,总结在一个方法体中:

    /**
     * 获取项目跟路径,http://localhost:8080/myproject
         *@returns
     */
    var getCurUrlInfo = function () {
       //获取当前网址,如: http://localhost:8088/myproject/index/login.html
        var curWwwUrl=window.document.location.href;
        
        //获取主机地址之后的目次,如: myproject/index/login.html
        var UrlName=window.document.location.Urlname;
        var hostUrlIndex=curWwwUrl.indexOf(UrlName);
        
        //获取主机地址,如: http://localhost:8088
        var localhostUrl=curWwwUrl.substring(0,hostUrlIndex);
        
        //获取带"/"的项目名,如:/myproject
        var projectName=UrlName.substring(0,UrlName.substr(1).indexOf("/")+1);
        
        return(localhostUrl+projectName);
    }

 

推荐阅读