JavaScript获取当前Web根目录的代码,很实用:
//获得当前目录
function getRootpath(){
//获得当前文件链接地址,如:http://localhost:8080/newjsp.jsp
var strFullPath=window.document.location.href;
//获得当前文件名称,如:/newjsp.jsp
var strPath=window.document.location.pathname;
//获得strPath文件链接地址前的所有字符
var pos=strFullPath.indexOf(strPath);
//获得从第1个字符到pos的地址,如:http://localhost:8080
var prePath=strFullPath.substring(0,pos);
var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
//输出http://localhost:8080
return(prePath+postPath);
}
获得当前目录的代码也是挺不少的,这是为了能够更加准确的获得当前web根目录或者域名