遇到引擎不能处理相对路径的问题,写了一个简单的相对路径转绝对路径的函数,留下备用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
function rel2Abs(path) { var _path = path; var locationStr = location.href.toString(); console.log(locationStr); var result = ''; var relative; while (relative = _path.substring(0, _path.indexOf("\/"))) { console.log(relative); if (relative === '.') { // 遇到. path去掉最前面的./ // 遇到.当前路径去掉/后面内容 _path = _path.substring(_path.indexOf('\/') + 1, _path.length); locationStr = locationStr.substring(0, locationStr.lastIndexOf('\/') + 1); } else if (relative === '..') { // 遇到.. path去掉最前面的../ // 遇到.. 当前路径去掉/xxx,然后再去掉/后面的内容 _path = _path.substring(_path.indexOf('\/') + 1, _path.length); locationStr = locationStr.substring(0, locationStr.lastIndexOf('\/')); locationStr = locationStr.substring(0, locationStr.lastIndexOf('\/') + 1); } else { break; } } var result = locationStr + _path; path = result; return result; }<code> |
原理很简单,只支持/,且遇到错误不能自动处理