js获得项目的根路径等的插件js代码,有效杜绝路径问题
(function(){ //获取contextPath var contextPath=getContextPath(); //获取basePath var basePath=getBathPath(); //将获取到的contextPath和basePatj分别赋值给window对象的g_context和g_basePath属性 window.g_contextPath=contextPath; window.g_basePath=basePath; })(); /** * 获得项目根路径,等价于jsp页面中 * <% String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/" %> */ function getBathPath(){ var curWwwPath=window.document.location.href; var pathName=window.document.location.pathname; var pos = curWwwPath.indexOf(pathName); var localhostPath = curWwwPath.substring(0, pos); var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1); return (localhostPath + projectName); } /** * 获取web应用的contextPath,等价于jsp页面中 * <% String path=request.getContextPath %> */ function getContextPath(){ return window.document.location.pathname.substring(0,window.document.location.pathname.indexOf("\/",1)); }