问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/12355979/how-to-work-around-the-base-tag