Уже была эта тема здесь, но всё же не ясно мне.
| Код | <HTML> <HEAD> <base href="http://site.com/testl/"/> <script>
function trim(text) { return text.replace(/^\s+|\s+$/g, ""); } /*This function converts short attributes to long using <base> */ //doc - document //url- url to convert function shortToLongAttrConverter() { //--This function CAN'T be call by user--// this.readBaseAttr = function(doc) { var aBases = doc.getElementsByTagName('base'); if (!aBases || !aBases.length || aBases[0].getAttribute('href') == '' || (typeof aBases[0] == 'undefined')) return {"protocol": location.protocol, "host": location.host, "path": location.pathname.split('/')}; var relativePath = aBases[0].getAttribute('href'); if (relativePath.indexOf('://') != -1) { var protocol = relativePath.substr(0, relativePath.indexOf('://')) + '://'; relativePath = relativePath.substr(relativePath.indexOf('://') + 3); } else var protocol = location.protocol; relativePath = relativePath.split('/'); if (relativePath[relativePath.length - 1].indexOf('.') != -1) relativePath.splice(relativePath.length - 1, 1); if (relativePath.length) host = relativePath.shift(); else host = location.host;
return {"protocol": protocol, "host": host, "path": relativePath}; }
/*Variable to save all about host*/ var path;
//---This function can be call by the user---// /*Get short link and convert it to long attribute*/ this.getNewUrl = function (doc, url) { alert("Old link: "+url); var test_url=trim(url); test_url=test_url.toLowerCase(); if((test_url.indexOf('javascript:')!==0) && (test_url.indexOf('vbscript:')!==0)) { path = this.readBaseAttr(doc); if (url.search('://') != -1){ alert("New link(1):"+url); return url; } var j = path.path.length - 1; var local_path = url.split('/'); var new_url = ''; for (var i = local_path.length - 1; i >= 0; i--) { if (local_path[i] == '..' && path.path[j]) { new_url = path.path[j] + new_url; j--; } else { new_url = local_path[i] + new_url; } } url = new_url; if (url.substr(0, 2) == './') { alert("New link(2):"+path.protocol + path.host + '/' + url.substr(2)); return path.protocol + path.host + '/' + url.substr(2); } if (url.charAt(0) == '/') { alert("New link(3): "+path.protocol + path.host + url); return path.protocol + path.host + url; } alert("New link(4): "+path.protocol + path.host + '/' + url); return path.protocol + path.host + '/' + url; } else { alert("New link(5): "+url); return url; } } }
</script>
</HEAD>
<BODY> <form> Должно вернуть полный путь. <input type="button" value="shortToLongAttrConverter" onClick="new shortToLongAttrConverter().getNewUrl(document,'../my/2.gif')"><BR> </form> </BODY> </HTML>
|
Преобразовывает относительные линки в абсолютные. И каким-то образом работает, но до того момента когда я вот так явно пытаюсь вызвать функцию и посмотереть результат. Видимо ошибка где-то в | Код | for (var i = local_path.length - 1; i >= 0; i--) { if (local_path[i] == '..' && path.path[j]) { new_url = path.path[j] + new_url; j--; } else { new_url = local_path[i] + new_url; } }
|
но я ума не приожу что тут не так. Помогите до ума довести
|