javascript获得当前文档的相对路径
function fRelativePath(sRelative) {
// shawl.qiu code, return string
var sUrl = document.URL;
sUrl = sUrl.replace(/^.*?\:\/\/[^\/]+/, "").replace(/[^\/]+$/, "");
if(!sRelative){return sUrl;}
if(!/\/$/.test(sUrl)){sUrl += "/";}
if(/^\.\.\//.test(sRelative)) {
var Re = new RegExp("^\\.\\.\\/"), iCount = 0;
while(Re.exec(sRelative)!=null) {
sRelative = sRelative.replace(Re, "");
iCount++;
}
for(var i=0; i<iCount; i++){sUrl = sUrl.replace(/[^\/]+\/$/, "");}
if(sUrl=="") return "/";
return sUrl+sRelative;
}
sRelative = sRelative.replace(/^\.\//, "");
return sUrl+sRelative;
} // end function fRelativePath(sRelative)
// shawl.qiu code, return string
var sUrl = document.URL;
sUrl = sUrl.replace(/^.*?\:\/\/[^\/]+/, "").replace(/[^\/]+$/, "");
if(!sRelative){return sUrl;}
if(!/\/$/.test(sUrl)){sUrl += "/";}
if(/^\.\.\//.test(sRelative)) {
var Re = new RegExp("^\\.\\.\\/"), iCount = 0;
while(Re.exec(sRelative)!=null) {
sRelative = sRelative.replace(Re, "");
iCount++;
}
for(var i=0; i<iCount; i++){sUrl = sUrl.replace(/[^\/]+\/$/, "");}
if(sUrl=="") return "/";
return sUrl+sRelative;
}
sRelative = sRelative.replace(/^\.\//, "");
return sUrl+sRelative;
} // end function fRelativePath(sRelative)