У меня есть якорный тег с локальным значением href и функция JavaScript, которая использует значение href, но направляет его в несколько другое место, чем обычно. Тег выглядит как
<a onclick="return follow(this);" href="sec/IF00.html"></a>
и функцию JavaScript, которая выглядит как
baseURL = 'http://www.someotherdomain.com/';
function follow(item) {
location.href = baseURL + item.href;
}
I would expect that item.href
would just return a short string of "sec/IF00.html", but instead it returns the full href, "http://www.thecurrentdomain.com/sec/IF00.html". Is there a way that я can pull out just the short href as put in the anchor <a>
tag? Or do я lose that by natural HTML behavior?
I suppose я could use a string manipulation to do this, but it gets tricky because my local page may actually be "http://www.thecurrentdomain.com/somedir/somepath/sec/IF00.html", and my href field may or may not have a subdirectory in it (for ex href="page.html"
vs. href="sub/page.html"
), so я cannot always just remove every thing before the last slash.
Вы можете задаться вопросом, почему я спрашиваю об этом, и это потому, что он просто сделает страницу намного чище. Если невозможно получить только короткий href (как указано в теге anchor <a>
), то я, вероятно, мог бы просто вставить дополнительное поле в тег, например link="sec/IF00.html"
, но опять же, это было бы немного грязнее.