html页面中怎么获取项目根目录和引入js和css?

 我来答
情栀py
2017-12-27 · TA获得超过2835个赞
知道小有建树答主
回答量:30
采纳率:0%
帮助的人:3748
展开全部

<script src="../../../../../scripts/***.js></script>

除了如上边那种用多个父路径外还有没有别的方法直接获取项目跟路径。获取项目根目录

在jsp里去<%=request.getContextPath()%>这就是项目的根路径了,是到项目这层的。

js的引入方法如下:

<script src="<%=request.getContextPath() %> /home/test.js"></script>

css的引入方法:

<link href="<%=request.getContextPath() %>/css/one.css" rel="stylesheet" type="text/css">。/js获取项目根路径,如: http://localhost:8080/ems

getRootPath:function () {

//获取当前网址,如: http://localhost:8080/ems/Pages/Basic/Person.jsp

var curWwwPath = window.document.location.href;

//获取主机地址之后的目录,如: /ems/Pages/Basic/Person.jsp

var pathName = window.document.location.pathname;

var pos = curWwwPath.indexOf(pathName);

//获取主机地址,如: http://localhost:8080

var localhostPath = curWwwPath.substring(0, pos);

//获取带"/"的项目名,如:/ems

var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);

return(localhostPath + projectName);

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式