javascript 獲取當前部署項目路徑


javascript 獲取當前部署項目路徑

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

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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM