如何解决< base>标签? [英] How to work around the <base> tag?
问题描述
可能存在重复:
如何在HTML链接中使用JavaScript变量
我正在处理的网站的< base>
标记指向预先建立的网址。我想要做的就是绕过< base>
标签,通过使用下面的trueURL来查找网页的网址。
我需要这个变量,因为我想构建一些指向网页不同部分的内部锚。
我不知道我应该如何使用存储在我的trueURL变量中的url。
var trueURL =是否可以使用它,然后向url添加额外的扩展名以使其指向我的锚? window.location.href;
以下是我想要获得的内容:
< html>
< 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>
因此,最后我想让link1看起来像trueURL#link1。
我认为这会产生您要查找的效果:
< HTML>
< base href =http://www.example.comtarget =_ 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>
然而,通过javascript做这件事的一个问题是,抓取工具无法关注这些链接,所以你可能会采取搜索引擎优化命中。
理想的解决方案可能是提供绝对路径服务器端,而不是使用javascript。
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.
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.
这篇关于如何解决< base>标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!