-1

Possible Duplicate:
How to use JavaScript variable in an HTML link

The website that I am working on has a <base> tag point to a pre-established URL. What I would like to do is get around the <base> tag by using the trueURL bellow to find the url of the webpage.

I need this variable because i would like to construct some internal anchors that will point to different parts of the webpage.

The issue that im having is that i don't know how i should use the url that i store in my trueURL variable. Is it possible to use it and then add extra extensions to the url to get it to point to my anchors?

var trueURL = window.location.href;

The following is what I would like to obtain :

    <html>

    <ol>
        <li>
            <a href= [trueURL] + "#link1">Link1</a>
        </li>

        <li>
            <a href= [trueURL] + "#link2">Link2</a>
        </li>

        <li>
            <a href= [trueURL] + "#link3">Link2</a>
        </li>
    </ol>

    </html>

Therefore in the end i would like to have link1, for example, to look like trueURL#link1.

Community
  • 1
  • 1
sSmacKk
  • 1,033
  • 4
  • 15
  • 30
  • i think the issue with that is the tag ... so if base tag is set to wwww.example.com ... since that's a relative link, it would link to wwww.example.com#link2 – JasonStoltz Sep 10 '12 at 16:52
  • Although I know this wasn't your decision, it's worth reading this question and it's answers to understand the behaviors and quirks of `base`: [Is it recommended to use the html tag?](http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag) – Jared Farrish Sep 10 '12 at 17:07
  • 2
    Why are you starting an almost [identical question](http://stackoverflow.com/questions/12314799/how-to-use-javascript-variable-in-an-html-link)? – freefaller Sep 10 '12 at 17:07

1 Answers1

0

I think this would have the effect you're looking for:

<html>
<base href="http://www.example.com" target="_blank" />
<script type="text/javascript">
var trueUrl = document.location.href;
</script>
<ol>
    <li>
        <a href="#" onclick="window.location = trueUrl + '#link1'; return false">Link1</a>
    </li>
    <li>
        <a href="#" onclick="window.location = trueUrl + '#link2'; return false">Link2</a>
    </li>
    <li>
        <a href="#" onclick="window.location = trueUrl + '#link3'; return false">Link3</a>
    </li>
</ol>
</html>

However, the one issue with doing this via javascript, is that crawlers won't be able to follow those links, so you potentially take an SEO hit.

The ideal solution probably be to provide an absolute path server side, rather than using javascript.

JasonStoltz
  • 2,790
  • 4
  • 23
  • 36