日期:2014-05-16  浏览次数:20385 次

[转]JS获得当前项目的根路径

来源:?http://zghbwjl.blog.163.com/blog/static/1203366722010714103150595/

两种方法,其实一样,第一种:

<script>
function getRootPath(){
var strFullPath=window.document.location.href;
var strPath=window.document.location.pathname;
var pos=strFullPath.indexOf(strPath);
var prePath=strFullPath.substring(0,pos);
var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
return(prePath+postPath);
}

alert (getRootPath());
</script>

第二种:

<script type="text/javascript">
function getRootPath()
{
var pathName = window.location.pathname.substring(1);
var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
return window.location.protocol + '//' + window.location.host + '/'+ webName + '/';
}
alert (getRootPath());
</script>
?