
JS 中如何获取站点根目录文件的位置 20
我用JS写一个ajax的程序,如果要获取服务网页的地址,确定是上层的可以用../Server.aspx的地址来确定。但如果js程序所在的html在一个随机的地方,不知道目...
我用JS写一个ajax的程序,如果要获取服务网页的地址,确定是上层的可以用 ../Server.aspx 的地址来确定。但如果js程序所在的html在一个随机的地方,不知道目标服务程序,只知道它位于网站的根目录,怎么确定位置?
展开
2个回答
展开全部
先获取当前的全路径,然后对全路径进行分析获取到跟路径
//js获取项目根路径,如: http://localhost:8083/uimcardprj
function getRootPath(){
//获取当前网址,如: 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);
}
展开全部
<script type="text/javascript">
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>
你放到你网站试一下,应该提示的就是你网站的根目录,路径自己拼接下
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>
你放到你网站试一下,应该提示的就是你网站的根目录,路径自己拼接下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询