您的位置:首页 > 产品设计 > UI/UE

[js]js中获取 <%=request.getContextPath()%>

2013-02-01 16:07 501 查看
参考地址:http://hexudonghot.blog.163.com/blog/static/532043422012112264411234/

在jsp中获取context path或者basePath 是很容易的。在javascript中也可以获取,并且有的时候也很有必要。

    <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

在单独的javascript中不能使用<%= basePath%>类似这样的java  script所以,只能用javascript来获取此context path.
可以用下面的代码来实现。

var localObj = window.location;

var contextPath = localObj.pathname.split("/")[1];

var basePath = localObj.protocol+"//"+localObj.host+"/"+contextPath;

var server_context=basePath;

两种实现的方法用在不同的环境中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: