javascript 擷取當前部署項目路徑

來源:互聯網
上載者:User

標籤:span   tle   return   通過   rip   project   UI   ide   ati   

=========================================

javascript擷取當前部署項目路徑:

主要用到Location 對象,包含有關當前 URL 的資訊,是 Window 對象的一個部分,可通過 window.location 屬性來訪問。

方法一 (window.document.location.href/window.document.location.pathname) ------------轉自網路

 

function getRootPath_web() {            //擷取當前網址,如: http://localhost:8083/uimcardprj/share/meun.jsp            var curWwwPath = window.document.location.href;            //擷取主機地址之後的目錄,如: uimcardprj/share/meun.jsp            var pathName = window.document.location.pathname;            var pos = curWwwPath.indexOf(pathName);            //擷取主機地址,如: http://localhost:8083            var localhostPaht = curWwwPath.substring(0, pos);            //擷取帶"/"的項目名,如:/uimcardprj            var projectName = pathName.substring(0, pathName.substr(1).indexOf(‘/‘) + 1);            return (localhostPaht + projectName);        }

 //js擷取項目根路徑,如: http://localhost:8083/uimcardprj

=====================================

 

 

 

方法二(window.location.pathname/window.location.protocol/window.location.host)

function getRootPath_dc() {            var pathName = window.location.pathname.substring(1);            var webName = pathName == ‘‘ ? ‘‘ : pathName.substring(0, pathName.indexOf(‘/‘));            if (webName == "") {                return window.location.protocol + ‘//‘ + window.location.host;            }            else {                return window.location.protocol + ‘//‘ + window.location.host + ‘/‘ + webName;            }        }

註:

 1、document默示的是一個文檔對象,window默示的是一個視窗對象,一個視窗下可以有多個文檔對象。
  所以一個視窗下只有一個window.location.href,然則可能有多個document.URL、document.location.href------------轉自網路

 2、window.location.href和document.location.href可以被賦值,然後跳轉到其它頁面,document.URL只能讀不克不及寫------------轉自網路

 3、Location 對象詳細資料參考w3school http://www.w3school.com.cn/jsref/dom_obj_location.asp

grails擷取當前項目路徑:

System.getProperty("user.dir");//    /Users/hongmei1/ideaWork/wechatShow   

 grails擷取當前項目部署路徑:

// 擷取項目部署全部路徑  http://localhost:8080/wechatShow/WEB-INF/classes/String path = this.class.getClassLoader().getResource("").getPath();//截取項目路徑int end = path.length() - "WEB-INF/classes/".length();//http://localhost:8080/wechatShow/path = path.substring(0, end);

 

javascript 擷取當前部署項目路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.