//立即执行的js
(function() {
//获取contextPath
var contextPath = getContextPath();
//获取basePath
var basePath = getBasePath();
//将获取到contextPath和basePath分别赋值给window对象的g_contextPath属性和g_basePath属性
window.g_contextPath = contextPath;
window.g_basePath = basePath;
})(); /**
* @author 孤傲苍狼
* 获得项目根路径,等价于jsp页面中
* <%
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
* 使用方法:getBasePath();
* @returns 项目的根路径
*
*/
function getBasePath() {
var curWwwPath = window.document.location.href;
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
var localhostPath = curWwwPath.substring(, pos);
var projectName = pathName.substring(, pathName.substr().indexOf('/') + );
return (localhostPath + projectName);
} /**
* @author 孤傲苍狼
* 获取Web应用的contextPath,等价于jsp页面中
* <%
String path = request.getContextPath();
%>
* 使用方法:getContextPath();
* @returns /项目名称(/EasyUIStudy_20141104)
*/
function getContextPath() {
return window.document.location.pathname.substring(, window.document.location.pathname.indexOf('\/', ));
};

我从别人那里偷过来的:

https://home.cnblogs.com/u/xdp-gacl/

js中获取项目路径的小插件的更多相关文章

  1. jsp中的js中获取项目路径的方法

    在jsp中加上 <% String path = request.getContextPath(); String basePath = request.getScheme()+":/ ...

  2. JAVA类中获取项目路径

    在java web项目中获取项目的src/main/resource下的文件路径 当前类名.class.getClassLoader().getResource("/").getP ...

  3. linux环境下在springboot项目中获取项目路径(用于保存文件等)

    //application.properties中设置:(file.path=static/qrfile/)//保存到static文件夹下的qrfile目录@Value("${file.pa ...

  4. jsp和js中获取项目路径名

    一.jspa.<%=request.getContextPath()%>//结果:/projectNameb.${pageContext.request.contextPath}//结果: ...

  5. 纯html页面中js如何获得项目路径

    js,全称javascript,不过虽然是以java开头,不过与java一点关系都没有. js和java有如下区别: (1)js是浏览器端的语言,而java是服务器端的语言. (2)js是动态语言,j ...

  6. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  7. 小程序 js中获取时间new date()的用法(网络复制过来自用)

    js中获取时间new date()的用法   获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获 ...

  8. Java中获取项目根路径和类加载路径的7种方法

    引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. ...

  9. 关于在JSP页面中为什么一定要用${pageContext.request.contextPath}来获取项目路径,而不能用${request.contextPath}?

    这里的疑问在于pageContext和request都是JSP中的内置对象之一,为什么不直接用${request.contextPath}来获取项目路径? 出现这种疑问,其实是将JSP的内置对象和EL ...

随机推荐

  1. easyui 删除数据表格

    1 最直接的方法: 返回的数据格式               Object rows:Array[3] 0:Object 1:Object 2:Object length:3 __proto__:A ...

  2. opencl 学习资源

    1.AMD  opencl-optimization-guide http://developer.amd.com/tools-and-sdks/opencl-zone/amd-accelerated ...

  3. An unknown server-side error occurred while processing the command.处理

    在调用resetAPP()时,报错:An unknown server-side error occurred while processing the command. 怎么解决呢?请看: 额,Ap ...

  4. 基于SourceTree 下的 Git Flow 模型

    基于SourceTree 下的 Git Flow 模型 1. sourceTree  是一个开源的git 图形管理工具,可下载mac版本,windows版本 2. Git Flow 是一套使用Git进 ...

  5. webstorm注释写出的提示

    写出这种代码提示的方法是 在一个函数上方打出 /** 然后敲回车就出出来 没达到上面的效果,自己手动写上即可. 这样的好处是: 当你写代码用到此方法的时候会有参数类型提示,如图

  6. Ubuntu 系统下 mongodb 安装和配置

    安装 MongoDB sudo apt-get install mongodb sudo apt-get install mongodb 关闭/启动 sudo service mongodb stop ...

  7. 在excel单元格中提取信息

    平时在excel中处理数据的时候,肯定会遇到在单元格提取信息的情况,比如在地址中提取省.市.地区等,如果数据源内容规整的话,可以直接使用left().right().mid()等函数直接提取,但是大多 ...

  8. Linux基础:Grep查询&AWK查询

    Grep:搜索文件内匹配指定内容的行 常用的Grep搜索方法: 1. grep "普通搜索内容" file( 或者 cat file|grep "普通搜索内容" ...

  9. JS禁止右键

    function cancelMouse(){return false;}document.oncontextmenu = cancelMouse;

  10. Java文件编码格式转换

    转自博文<Java文件编码格式转换>: 默认被转换的格式为GBK,转换成的格式为UTF-8 import info.monitorenter.cpdetector.CharsetPrint ...