function getContextPath() { var contextPath = document.location.pathname; alert(contextPath+"=====") var index =contextPath.substr(1).indexOf("/"); contextPath = contextPath.substr(0,index+1); delete index; return contextPath; } alert(getContextPath()+'==========')