---
title: "获取服务器路径"
author: "heyafang"
date: "2014-05-21T12:00:40Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2014-05-21T17:56:03Z"
id: 2069066
url: "https://zenman.iteye.com/blog/2069066"
---

function getRootPath(){  
    //获取当前网址，如： [http://localhost:8080/CSchool/index.jsp](http://localhost:8080/CSchool/index.jsp)  
    var curWwwPath=window.document.location.href;  
    //获取主机地址之后的目录，如：CSchool/index.jsp  
    var pathName=window.document.location.pathname;  
    var pos=curWwwPath.indexOf(pathName);  
    //获取主机地址，如： [http://localhost:8080](http://localhost:8080/)  
    var localhostPaht=curWwwPath.substring(0,pos);  
    //获取带"/"的项目名，如：//CSchool  
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
    return(localhostPaht+projectName);  
}

document.getElementById("hwfc").onclick=function(){this.href=fic;}; 给&lt;a&gt;赋超链接