什么是对HTML< base>的建议?标签? [英] What are the recommendations for html <base> tag?

查看:197
本文介绍了什么是对HTML< base>的建议?标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未见过 < ; base> HTML标记实际上在任何地方使用过。它有什么缺陷意味着我应该避免它?



我从来没有注意到它在现代生产网站(或任何网站)上使用的事实使我尽管它似乎可能有用于简化我网站链接的有用的应用程序。






编辑



使用基本标签几周后,我最终发现了一些使用基本标签的主要陷阱,这使得它比它更不理想第一次出现。基本上,基本标签下的 href ='#topic' href =''非常不符合它们的默认行为,而且这种默认行为的变化可能会以非预期的方式轻易地使第三方库不受控制非常不可靠,因为它们将在逻辑上取决于默认行为。处理大型代码库时,这些更改通常很微妙,并且会导致不明显的问题。我从那以后创建了一个答案,详细说明了我所经历的问题。因此,在您承诺广泛部署< base> 之前,请自行测试链接结果,这是我的新建议!

解决方案

基本标记效果的细分:



基本标记似乎具有一些非直观的效果,我建议您在依赖< base> 之前了解结果并自行测试它们!因为我发现他们之后尝试使用基本标记来处理具有不同URL的本地网站,并且仅在发现之后出现问题效果,所以我感到不得不创建这些潜在的摘要 $ b

我会使用< base href =http://www.example.com/ other-subdirectory /> 作为我在下面的例子中的例子,并假装代码所在的页面是 http://localsite.com/original-subdirectory



专业:



没有链接或指定的锚点或空白hrefs将指向原始子目录,除非明确指出:
基本标记使一切链接不同,包括相同页面的锚链接而不是基本标记的网址,例如:



  • < a href ='#top-of -page'title ='Some title'>到th的顶部的链接通过命名定位< / a>


    变为

    < a href = 'http://www.example.com/other-subdirectory/#top-of-page'title ='Some title'>指向完全不同的基本页上的#named-anchor的链接< / a>


  • < a href ='?update = 1'title ='Some title'>转到此页< / a>


    变为

    < a href ='http: //www.example.com/other-subdirectory/?update=1'title ='Some title'>指向基本代码页面的链接,而不是< / a>




通过一些工作,您可以在您控制的链接上修复这些问题,方法是明确指定这些链接链接到他们的页面但是当您将第三方库添加到依赖标准行为的组合中时,它可能很容易导致一团糟。



次要:



需要条件注释的IE6修复:需要con ie6避免搞砸dom层次结构的注释,即< base href =http://www.example.com/><! - [if lte IE 6]> ;< / code>< / code>> BalusC< / code>在上面的回答中提及。
$ b

因此,总体而言,主要问题会使用棘手的问题,除非您对每个链接都有完整的编辑控制权限,而正如我最初所担心的那样,这使得它比它的价值更麻烦。现在我不得不重新写下它的所有用法! :p



使用片段/哈希时测试问题的相关链接:

http://www.w3.org/People/mimasa/test/base/



http:// www。 w3.org/People/mimasa/test/base/results






编辑Izzy: 对于你们所有人来说,和我一样对这些评论意见一无所知:



我自己测试了一下,结果如下:




  • 尾随斜杠或不对这里给出的例子( #anchor ?query 只会附加到指定的< BASE> )。

  • 然而,它对相对链接有所不同:省略了斜线, other.html dir / other.html 将从 DOCUMENT_ROOT 与给定示例 [per which browser?] / other-subdirectory sup> [per which browser?] 。



因此,对于相关链接, BASE 对于移动的页面可以正常工作 - 而anchor和?queries 需要明确指定文件名(使用 BASE 有一个斜线,或者最后一个元素与它所用文件的名字不相对应)。

把它看作< BASE> ,将完整的网址替换为文件本身(而不是它所在的目录)对。假设这个例子中使用的文件是 other-subdirectory / test.html (在它移动到新位置之后),正确的规范应该是:


< base href =http://www.example.com/other -subdirectory / test.html >




- 等瞧, #anchor ?query other.html / code>, very / other.html /completely/other.html


I've never seen <base> HTML tag actually used anywhere before. Are there pitfalls to its use that means I should avoid it?

The fact that I have never noticed it in use on a modern production site (or any site) makes me leery of it, though it seems like it might have useful applications for simplifying links on my site.


Edit

After using the base tag for a few weeks, I did end up finding some major gotchas with using the base tag that make it much less desirable than it first appeared. Essentially, the changes to href='#topic' and href='' under the base tag are very incompatible with their default behavior, and this change from the default behavior could easily make third party libraries outside of your control very unreliable in unexpected ways, since they will logically depend on the default behavior. Often the changes are subtle and lead to not-immediately-obvious problems when dealing with a large codebase. I have since created an answer detailing the issues that I experienced below. So test the link results for yourself before you commit to a widespread deployment of <base>, is my new advice!

解决方案

Breakdown of the effects of the base tag:

The base tag appears to have some non-intuitive effects, and I recommend being aware of the outcomes and testing them for yourself before relying on <base>! Since I've discovered them after trying to use the base tag to handle local sites with differing urls and only found out the problematic effects after, to my dismay, I feel compelled to create this summary of these potential pitfalls for others.

I'll use a base tag of: <base href="http://www.example.com/other-subdirectory/"> as my example in the cases below, and will pretend that the page that the code is on is http://localsite.com/original-subdirectory

Major:

No links or named anchors or blank hrefs will point to the original subdirectory, unless that is made explicit: The base tag makes everything link differently, including same-page anchor links to the base tag's url instead, e.g:

  • <a href='#top-of-page' title='Some title'>A link to the top of the page via a named anchor</a>
    becomes
    <a href='http://www.example.com/other-subdirectory/#top-of-page' title='Some title'>A link to an #named-anchor on the completely different base page</a>

  • <a href='?update=1' title='Some title'>A link to this page</a>
    becomes
    <a href='http://www.example.com/other-subdirectory/?update=1' title='Some title'>A link to the base tag's page instead</a>

With some work, you can fix these problems on links that you have control over, by explicitly specifying that these links link to the page that they are on, but when you add third-party libraries to the mix that rely on the standard behavior, it can easily cause a big mess.

Minor:

IE6 fix that requires conditional comments: Requires conditional comments for ie6 to avoid screwing up the dom hierarchy, i.e. <base href="http://www.example.com/"><!--[if lte IE 6]></base><![endif]--> as BalusC mentions in his answer above.

So overall, the major problem makes use tricky unless you have full editing control over every link, and as I originally feared, that makes it more trouble than it's worth. Now I have to go off and rewrite all my uses of it! :p

Related links of testing for issues when using "fragments"/hashes:

http://www.w3.org/People/mimasa/test/base/

http://www.w3.org/People/mimasa/test/base/results


Edit by Izzy: For all of you running into the same confusion as me concerning the comments:

I've just tested it out myself, with the following results:

  • trailing slash or not, makes no difference to the examples given here (#anchor and ?query would simply be appended to the specified <BASE>).
  • It however makes a difference for relative links: omitting the trailing slash, other.html and dir/other.html would start at the DOCUMENT_ROOT with the given example [per which browser?], /other-subdirectory being (correctly) treated as file and thus omitted [per which browser?].

So for relative links, BASE works fine with the moved page – while anchors and ?queries would need the file name be specified explicitly (with BASE having a trailing slash, or the last element not corresponding to the name of the file it's used in).

Think of it as <BASE> replacing the full URL to the file itself (and not the directory it resides in), and you'll get things right. Assuming the file used in this example was other-subdirectory/test.html (after it moved to the new location), the correct specification should have been:

<base href="http://www.example.com/other-subdirectory/test.html">

– et voila, everything works as expected: #anchor, ?query, other.html, very/other.html, /completely/other.html.

这篇关于什么是对HTML&lt; base&gt;的建议?标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆