html 有哪些建议?标签?

发布于 2024-08-15 02:56:24 字数 599 浏览 18 评论 0 原文

我从未见过 HTML 标签 实际上以前在任何地方都使用过。它的使用中有哪些陷阱意味着我应该避免它?

事实上,我从未注意到它在现代生产站点(或任何站点)上使用,这让我对它持怀疑态度,尽管它似乎可能具有用于简化我站点上的链接的有用应用程序。


编辑

在使用基本标签几周后,我最终发现了使用基本标签的一些主要问题,这使得它比第一次出现时要差得多。本质上,对基本标记下的 href='#topic'href='' 的更改与其默认行为非常不兼容,并且对默认行为的这种更改很容易使您无法控制的第三方库以意想不到的方式非常不可靠,因为它们在逻辑上依赖于默认行为。通常,这些更改是微妙的,并且在处理大型代码库时会导致不立即明显的问题。此后,我创建了一个答案,详细说明了我在下面遇到的问题。因此,在您致力于广泛部署 之前,请自行测试链接结果,这是我的新建议!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

飞烟轻若梦 2024-08-22 02:56:25

使页面更容易离线查看;您可以将完全限定的 URL 放入基本标记中,然后您的远程资源将正确加载。

It makes pages easier for offline viewing; you can put the fully qualified URL in the base tag and then your remote resources will load properly.

只有影子陪我不离不弃 2024-08-22 02:56:25

哈希值“#”目前与基本元素一起用于跳转链接,但仅适用于最新版本的 Google Chrome 和 Firefox,不适用于 IE9。

IE9 似乎会导致页面重新加载,而不会跳转到任何地方。如果您在 iframe 外部使用跳转链接,同时指示框架加载框架内单独页面上的跳转链接,您将获得框架内加载的跳转链接页面的第二个副本。

The hash "#" currently works for jump links in conjunction with the base element, but only in the latest versions of Google Chrome and Firefox, NOT IE9.

IE9 appears to cause the page to be reloaded, without jumping anywhere. If you are using jump links on the outside of an iframe, while directing the frame to load the jump links on a separate page within the frame, you will instead get a second copy of the jump link page loaded inside the frame.

离去的眼神 2024-08-22 02:56:25

它可能不是很受欢迎,因为它不为人所知。我不会害怕使用它,因为所有主流浏览器都支持它。

如果您的网站使用 AJAX,您需要确保所有页面都正确设置,否则您可能会得到无法解析的链接。

只是不要在 HTML 4.01 Strict 页面中使用 target 属性。

It's probably not very popular because it's not well known. I wouldn't be afraid of using it since all major browsers support it.

If your site uses AJAX you'll want to make sure all of your pages have it set correctly or you could end up with links that cannot be resolved.

Just don't use the target attribute in an HTML 4.01 Strict page.

叹倦 2024-08-22 02:56:25

对于页面中内嵌的 SVG 图像,使用 base 标签时会出现另一个重要问题:

由于使用 base 标签(如上所述),您可以有效地放弃了使用相对哈希 URL 的能力,如

中那样,

因为它们将根据基本 URL 而不是当前文档的位置进行解析,因此不再是相对的。
因此,您必须将当前文档的路径添加到这些类型的链接中,如

所以base 标签看似积极的方面之一(即将长 URL 前缀从锚标签移开,并获得更好、更短的锚)对于本地哈希 URL 来说完全适得其反。

当在页面中内联 SVG 时,无论是静态 SVG 还是动态生成的 SVG,这尤其令人烦恼,因为在 SVG 中可能有很多此类引用 并且在大多数(但不是所有)用户代理实现上,一旦使用 base 标签,它们就会全部中断(Chrome 至少在撰写本文时仍然可以在这些场景中工作)。

如果您正在使用模板系统或其他工具链来处理/生成页面,我总是会尝试摆脱 base 标签,因为据我所知,它会给页面带来更多问题。表比它解决。

In the case of SVG images inlined in the page there is another important issue that arises when the base tag is used:

Since with the base tag (as noted above already) you effectively loose the ability to use relative hash URLs like in

<a href="#foo">

because they will be resolved against the base URL rather than the current document's location and thus are not relative anymore.
So you will have to add the path of the current document to these kinds of links like in

<a href="/path/to/this/page/name.html#foo">

So one of the seemingly positive aspects of the base tag (which is to move the long URL prefixes away from the anchor tag and get nicer, shorter anchors) completely backfires for local hash URLs.

This is especially annoying when inlining SVG in your page, be it static SVG or dynamically generated SVG because in SVG there can be a lot of such references and they will all break as soon as a base tag is used, on most, but not all user agent implementations (Chrome at least still works in these scenarios at the time of writing).

If you are using a templating system or another tool-chain that processes/generates your pages, I would always try to get rid of the base tag, because as I see it, it brings more problems to the table than it solves.

柏林苍穹下 2024-08-22 02:56:25

使用 AngularJS 时,BASE 标签悄悄地破坏了 $cookieStore,我花了一段时间才弄清楚为什么我的应用程序无法再写入 cookie。请注意...

Working with AngularJS the BASE tag broke $cookieStore silently and it took me a while to figure out why my app couldn't write cookies anymore. Be warned...

说不完的你爱 2024-08-22 02:56:25

另外,您应该记住,如果您在非标准端口运行 Web 服务器,您也需要在基本 href 中包含端口号:

<base href="//localhost:1234" />  // from base url
<base href="../" />  // for one step above

Also, you should remember that if you run your web server at non-standard port you need to include port number at base href too:

<base href="//localhost:1234" />  // from base url
<base href="../" />  // for one step above
李不 2024-08-22 02:56:25

我从来没有真正看到使用它的意义。提供的优势非常有限,甚至可能使事情变得更难使用。

除非您碰巧有数百或数千个链接,全部都指向同一个子目录。那么它可能会为您节省几个字节的带宽。

事后想起来,我似乎记得 IE6 中的标签有一些问题。您可以将它们放置在身体的任何位置,将网站的不同部分重定向到不同的位置。这个问题在 IE7 中得到了修复,导致很多网站崩溃。

I've never really seen a point in using it. Provides very little advantage, and might even make things harder to use.

Unless you happen to have hundreds or thousands of links, all to the same sub-directory. Then it might save you a few bytes of bandwidth.

As an afterthought, I seem to recall there being some problem with the tag in IE6. You could place them anywhere in the body, redirecting different portions of the site to different locations. This was fixed in IE7, which broke a lot of sites.

沙沙粒小 2024-08-22 02:56:25

要记住一件事:

如果您开发一个要在 iOS 上的 UIWebView 中显示的网页,那么您必须使用 BASE 标签。否则它根本就行不通。 JavaScript、CSS、图像 - 它们都不能与 UIWebView 下的相对链接一起使用,除非指定了 BASE 标签。

我以前也被这个问题困扰过,直到我发现为止。

One thing to keep in mind:

If you develop a webpage to be displayed within UIWebView on iOS, then you have to use BASE tag. It simply won't work otherwise. Be that JavaScript, CSS, images - none of them will work with relative links under UIWebView, unless tag BASE is specified.

I've been caught by this before, till I found out.

却一份温柔 2024-08-22 02:56:25

还有一个使用基本标签的网站,并且发生了所描述的问题。 (升级 jquery 后),能够通过像这样的选项卡 URL 来修复它:

<li><a href="{$smarty.server.REQUEST_URI}#tab_1"></li>

这使它们成为我使用的“本地”

引用:

http://bugs.jqueryui.com/ticket/7822
http://htmlhelp.com/reference/html40/head/base.html
http://tjvantoll.com/ 2013/02/17/using-jquery-ui-tabs-with-the-base-tag/

have also a site where base - tag is used, and the problem described occured. ( after upgrading jquery ), was able to fix it by having tab urls like this:

<li><a href="{$smarty.server.REQUEST_URI}#tab_1"></li>

this makes them "local"

references i used:

http://bugs.jqueryui.com/ticket/7822
http://htmlhelp.com/reference/html40/head/base.html
http://tjvantoll.com/2013/02/17/using-jquery-ui-tabs-with-the-base-tag/

错爱 2024-08-22 02:56:25

基本 href 示例

说一个带有链接的典型页面:

<a href=home>home</a> <a href=faq>faq</a> <a href=etc>etc</a>

. 以及指向 diff 文件夹的链接:

..<a href=../p2/home>Portal2home</a> <a href=../p2/faq>p2faq</a> <a href=../p2/etc>p2etc</a>..

使用 base href,我们可以避免重复基本文件夹:

<base href=../p2/>
<a href=home>Portal2-Home</a> <a href=faq>P2FAQ</a> <a href=contact>P2Contact</a>

所以这是一个胜利..但是页面经常包含区分基本文件夹的网址并且当前的网络支持 >每页只有一个基本href,因此,当基本hrefed不重复时,胜利很快就会消失,例如:

<a href=../p1/home>home</a> <a href=../p1/faq>faq</a> <a href=../p1/etc>etc</a>
<!--.. <../p1/> basepath is repeated -->

<base href=../p2>
<a href=home>Portal2-Home</a> <a href=faq>P2FAQ</a> <a href=contact>P2Contact</a>


Conclusion

基本目标可能有用.) 基本 href 没有用,因为:

  • 页面同样 WET 自:
    • 默认基础[–父文件夹] ⇌完美(除非不必要/罕见的例外

Base href example

Say a typical page with links:

<a href=home>home</a> <a href=faq>faq</a> <a href=etc>etc</a>

.and links to a diff folder:

..<a href=../p2/home>Portal2home</a> <a href=../p2/faq>p2faq</a> <a href=../p2/etc>p2etc</a>..

With base href, we can avoid repeating the base folder:

<base href=../p2/>
<a href=home>Portal2-Home</a> <a href=faq>P2FAQ</a> <a href=contact>P2Contact</a>

So that's a win.. yet pages too-often contain urls to diff bases And the current web supports only one base href per page, so the win is quickly lost as bases that aint base∙hrefed repeats, eg:

<a href=../p1/home>home</a> <a href=../p1/faq>faq</a> <a href=../p1/etc>etc</a>
<!--.. <../p1/> basepath is repeated -->

<base href=../p2>
<a href=home>Portal2-Home</a> <a href=faq>P2FAQ</a> <a href=contact>P2Contact</a>


Conclusion

(Base target might be useful.) Base href is useless as:

  • page is equally WET since:
    • default base [–parent folder] ⇌ perfect (unless unnecessary/rare exceptions 𝒞1 & 𝒞2).
    • current web ⇌ multiple base hrefs unsupported.

Related

梦与时光遇 2024-08-22 02:56:25

我找到了一种使用 和基于锚点的链接的方法。您可以使用 JavaScript 来保持 #contact 等链接正常工作。我在一些视差页面中使用了它,它对我有用。

<base href="http://www.mywebsite.com/templates/"><!--[if lte IE 6]></base><![endif]-->

...content...

<script>
var link='',pathname = window.location.href;
$('a').each(function(){
    link = $(this).attr('href');
    if(link[0]=="#"){
        $(this).attr('href', pathname + link);
    }
});
</script>

您应该在页面末尾使用

I have found a way to use <base> and anchor based links. You can use JavaScript to keep links like #contact working as they have to. I used it in some parallax pages and it works for me.

<base href="http://www.mywebsite.com/templates/"><!--[if lte IE 6]></base><![endif]-->

...content...

<script>
var link='',pathname = window.location.href;
$('a').each(function(){
    link = $(this).attr('href');
    if(link[0]=="#"){
        $(this).attr('href', pathname + link);
    }
});
</script>

You should use at the end of the page

爱*していゐ 2024-08-22 02:56:25

我的建议是不要在管理 URL 路径时使用 元素。为什么?

它只是用一个问题换另一个问题。如果没有基本元素,您可以使用您喜欢的任何路径系统作为相对路径和链接,而不必担心它们会损坏。当您将基本元素设置为路径时,您就被“锁定”了,无法设计所有 url 以在该路径上工作,现在必须将所有路径更改为从基本路径开始工作。坏主意!

这意味着您现在必须编写更长的路径并跟踪每个路径相对于该基础的位置。更糟糕的是......当使用 元素时,他们建议您使用完全限定的基本路径来支持旧版浏览器(“https://www.example.com/"),现在您已将域硬编码到页面中或使所有链接都依赖于有效的域路径。

另一方面,当您再次从网站中删除基本路径时,您现在可以再次自由地使用较短的相对路径,这些路径可以是完全限定的,使用来自根的绝对路径,或者使用真正相对于您所在的文件和文件夹。它更加灵活。最重要的是,像“#hello”这样的片段可以正常工作,无需任何额外的修复。再说一遍,人们正在制造不存在的问题。

另外,上面提到的使用基本 url 来帮助您将网页文件夹迁移到新的子文件夹位置的论点如今并不重要,因为大多数现代服务器允许您快速将任何子文件夹设置为任何域下的新应用程序根文件夹。 Web 应用程序的定义或“根”现在不受文件夹或域的限制。

整个辩论似乎有点愚蠢。所以我说忽略基本 url 并支持不使用它的旧本机服务器-客户端默认路径系统。

注意:如果您遇到的问题是由于某些新的 API 系统而导致控制路径,那么解决方案很简单...在 API 中的所有 url 和链接的路径保持一致。不要像 javascript API 小子那样依赖浏览器对基本或 HTML5 或更新的马戏团技巧的支持。只需一致地路径所有锚标记,您就永远不会遇到问题。此外,无论使用何种路径系统,您的 Web 应用程序都可以立即移植到新服务器。

旧的又是新的!其基本要素显然是要尝试创建一个解决方案来解决 20 年前网络世界中从未存在的问题,更不用说今天了。

My recommendation is NOT to use the <base> element in managing url paths. Why?

It just trades one problem for another. Without the base element you can use any path system you like for your relative paths and links without fear they will break. The minute you set the base element to a path you are "locked in" to designing all your url's to work off that path and will now have to change ALL paths to work from the base path. Bad idea!

That means you have to now write LONGER paths AND keep track of where each path is relative to this base. Worse.....when using the <base> element they recommend you use a fully qualified base path to support older browsers ("https://www.example.com/"), so now you've hard-coded your domain into your page or made all your links dependent on a valid domain path.

On the other hand, the minute you remove the base path again from your website, you are now free again to use shorter relative paths, which can be fully qualified, use absolute paths from the root, or use paths that are truly relative to the file and folder you are in. Its much more flexible. And best of all fragments like "#hello" work correctly without any additional fixes. Again, people are creating problems that don't exist.

Also, the argument above that your use base url's to help you migrate folders of web pages to new subfolder locations doesn't really matter today since most modern servers allow you to quickly set up any subfolder as a new application root folder under any domain. The definition or the "root" of a web application is not constrained by either folders or domains now.

It seems kind of silly this whole debate. So I say leave out base url and support the older native server-client default path system that does not use it.

Note: If the problem you have is controlling paths due to some new API system, the solution is simple...be consistent in how you path all your url's and links in your API. Don't rely on browser support of base or HTML5 or newer circus tricks like the javascript API kiddies do. Simply path all your anchor tags consistently and you will never have issues. Whats more, your web application is instantly portable to new servers regardless of path systems used.

Whats old is new again! The base element is clearly about trying to create a solution to a problem that never existed in the Web World 20 years ago, much less today.

把梦留给海 2024-08-22 02:56:25

我将在一长串理由中再添加一个论据,说明为什么您永远不应该设置 标记。正如许多其他人在这里指出的那样,设置 会更改 #anchor 和 ?query URL 的行为 - 它们附加到基本 href,而不是文档自己的 URL 的后备基础。

您可能认为设置 可以解决此问题,并且一切都会正常运行。你就错了。

在现实世界的网络上,?query=parameters 始终用于会话归因(如 Google Analytics 中)和许多其他事物。其中许多参数仅供客户端脚本使用;服务器不关心它们或对它们做任何事情。

假设您正在提供 example.com/landing-page,其中包含 和一个链接在页面上转到#menu。然后有人通过 URL example.com/landing-page?source=my-marketing-campaign 到达那里。

如果没有 ,当他们单击 #menu 时,浏览器将识别页内导航并立即跳转到该部分。

但是,由于您定义的基本 href example.com/landing-page 缺少查询参数 source=my-marketing-campaign,浏览器无法确定< /em> 这是一个页内链接。因此,单击 #menu 将触发 HTTP 请求,并且页面(及其所有未缓存的资源)将重新加载。最好的情况是,这对各方来说都是毫无意义的时间和带宽浪费。最坏的情况是,状态和数据可能会丢失。

对于静态网站来说,没有办法解决这个问题。如果您使用的是 CMS,您可能会想一下可以将 动态设置为请求 URL(包括所有参数)。虽然这可以解决刷新问题,但您最终会遇到缓存问题和引发噩梦的安全漏洞

底线: 会给你带来麻烦,无论你设置什么。不要这样做。

如果您担心恶意脚本可能会注入自己的 标记(例如在 nonce 重定向攻击),2022 年最好的解决方案似乎是添加 base-uri 指令到您的内容安全策略。

I'll add one more argument to the long list of reasons why you should never set a <base href> tag. As many others have noted here, setting <base href> changes the behavior of #anchor and ?query URLs — they get attached to the base href, not to the fallback base that is the document's own URL.

You might think that setting <base href="https://example.com/the/documents/own/url"> would solve this, and everything would behave normally. You'd be wrong.

On the real-world web, ?query=parameters are used all the time for session attribution (as in Google Analytics), and many other things. Many of these parameters are exclusively for client-side use by scripts; the server doesn't care or do anything with them.

Let's say you're serving example.com/landing-page, with a <base href="example.com/landing-page">, and a link on the page to #menu. Then someone arrives there via the URL example.com/landing-page?source=my-marketing-campaign.

Without the <base href>, when they click #menu the browser would recognize in-page navigation and jump instantly to that section.

But, since your defined base href example.com/landing-page is missing the query parameter source=my-marketing-campaign, the browser cannot be sure that it is an in-page link. Thus, clicking #menu will fire a new HTTP request and the page (and all its uncached assets) will reload. Best case scenario, this is a pointless waste of time and bandwidth on all sides. Worst case, state and data could be lost.

There's no way around this for a static site. If you're using a CMS you might think for a moment that you could dynamically set the <base href> to the request URL, all parameters included. While this would solve the refresh issue, you'd end up with caching troubles and a nightmare-inducing security vulnerability.

Bottom line: <base href> will cause you trouble, no matter what you set it to. Don't do it.

If you're concerned about the possibility of a malicious script injecting its own <base href="evil.com"> tag (for example in a nonce retargeting attack), the best solution in 2022 seems to be adding the base-uri directive to your Content Security Policy.

从此见与不见 2024-08-22 02:56:24

在决定是否使用 标签之前,您需要了解它的工作原理、用途以及含义,最后权衡利弊。


标记主要简化了在模板语言中创建相对链接的过程,因为您无需担心每个链接中的当前上下文。

例如,您可以执行以下操作

<base href="${host}/${context}/${language}/">
...
<link rel="stylesheet" href="css/style.css" />
<script src="js/script.js"></script>
...
<a href="home">home</a>
<a href="faq">faq</a>
<a href="contact">contact</a>
...
<img src="img/logo.png" />

,而不是

<link rel="stylesheet" href="/${context}/${language}/css/style.css" />
<script src="/${context}/${language}/js/script.js"></script>
...
<a href="/${context}/${language}/home">home</a>
<a href="/${context}/${language}/faq">faq</a>
<a href="/${context}/${language}/contact">contact</a>
...
<img src="/${context}/${language}/img/logo.png" />

请注意 值以斜杠结尾,否则它将被解释为相对于最后一个路径。


至于浏览器兼容性,这只会导致 IE 出现问题。 标记在 HTML 中指定为 not 具有结束标记 ,因此仅使用 < code> 没有结束标记。然而 IE6 却有不同的看法,在这种情况下, 标记之后的整个内容将被放置为 。 HTML DOM 树中的 base> 元素。这可能会导致 Javascript/jQuery/CSS 中出现乍一看无法解释的问题,即在 html>body 等特定选择器中完全无法访问元素,直到您在 HTML DOM 检查器中发现应该有一个 <代码>基(和<代码>头)之间。

常见的 IE6 修复是使用 IE 条件注释来包含结束标记:

<base href="http://example.com/en/"><!--[if lte IE 6]></base><![endif]-->

如果您不关心 W3 验证器,或者当您已经使用 HTML5 时,那么您可以自行关闭它,每个网络浏览器都支持它:

<base href="http://example.com/en/" />

关闭 标签也会立即修复 insanity 在无限循环中使用 src 中的相对 URI 请求

当您在 标记中使用相对 URI 时,会出现另一个潜在的 IE 问题,例如 < /code> 或 。这在 IE6/7/8 中会失败。然而,这并不完全是浏览器的错。在 标签中使用相对 URI 本身就是错误的。 HTML4 规范 声明它应该是绝对 URI ,因此从 http://https:// 方案开始。此内容已在 HTML5 规范中删除。因此,如果您使用 HTML5 并仅定位 HTML5 兼容浏览器,那么在 标记中使用相对 URI 应该没问题。


至于使用命名/哈希片段锚点(如 )、查询字符串锚点(如 和路径片段锚点,如 ,使用 标签基本上声明 与之相关的所有相对链接,包括那些类型的锚点。所有相对链接都不再与当前请求 URI 相关(如果没有 标记就会发生这种情况)。首先,这可能会让初学者感到困惑。要以正确的方式构建这些锚点,您基本上需要包含 URI,

<a href="${uri}#anchor">hash fragment</a>
<a href="${uri}?foo=bar">query string</a>
<a href="${uri};foo=bar">path fragment</a>

其中 ${uri} 基本上转换为 PHP 中的 $_SERVER['REQUEST_URI']${pageContext.request.requestURI},在 JSF 中为 #{request.requestURI}。应该注意的是,像 JSF 这样的 MVC 框架具有减少所有这些样板文件并消除对 的需要的标签。另请参阅 什么 URL用于链接/导航到其他 JSF 页面

Before deciding whether to use the <base> tag or not, you need to understand how it works, what it can be used for and what the implications are and finally outweigh the advantages/disadvantages.


The <base> tag mainly eases creating relative links in templating languages as you don't need to worry about the current context in every link.

You can do for example

<base href="${host}/${context}/${language}/">
...
<link rel="stylesheet" href="css/style.css" />
<script src="js/script.js"></script>
...
<a href="home">home</a>
<a href="faq">faq</a>
<a href="contact">contact</a>
...
<img src="img/logo.png" />

instead of

<link rel="stylesheet" href="/${context}/${language}/css/style.css" />
<script src="/${context}/${language}/js/script.js"></script>
...
<a href="/${context}/${language}/home">home</a>
<a href="/${context}/${language}/faq">faq</a>
<a href="/${context}/${language}/contact">contact</a>
...
<img src="/${context}/${language}/img/logo.png" />

Please note that the <base href> value ends with a slash, otherwise it will be interpreted relative to the last path.


As to browser compatibility, this causes only problems in IE. The <base> tag is in HTML specified as not having an end tag </base>, so it's legit to just use <base> without an end tag. However IE6 thinks otherwise and the entire content after the <base> tag is in such case placed as child of the <base> element in the HTML DOM tree. This can cause at first sight unexplainable problems in Javascript/jQuery/CSS, i.e. the elements being completely unreachable in specific selectors like html>body, until you discover in the HTML DOM inspector that there should be a base (and head) in between.

A common IE6 fix is using an IE conditional comment to include the end tag:

<base href="http://example.com/en/"><!--[if lte IE 6]></base><![endif]-->

If you don't care about the W3 Validator, or when you're on HTML5 already, then you can just self-close it, every webbrowser supports it anyway:

<base href="http://example.com/en/" />

Closing the <base> tag also instantly fixes the insanity of IE6 on WinXP SP3 to request <script> resources with an relative URI in src in an infinite loop.

Another potential IE problem will manifest when you use a relative URI in the <base> tag, such as <base href="//example.com/somefolder/"> or <base href="/somefolder/">. This will fail in IE6/7/8. This is however not exactly browser's fault; using relative URIs in the <base> tag is namely at its own wrong. The HTML4 specification stated that it should be an absolute URI, thus starting with the http:// or https:// scheme. This has been dropped in HTML5 specification. So if you use HTML5 and target HTML5 compatible browsers only, then you should be all fine by using a relative URI in the <base> tag.


As to using named/hash fragment anchors like <a href="#anchor">, query string anchors like <a href="?foo=bar"> and path fragment anchors like <a href=";foo=bar">, with the <base> tag you're basically declaring all relative links relative to it, including those kind of anchors. None of the relative links are relative to the current request URI anymore (as would happen without the <base> tag). This may in first place be confusing for starters. To construct those anchors the right way, you basically need to include the URI,

<a href="${uri}#anchor">hash fragment</a>
<a href="${uri}?foo=bar">query string</a>
<a href="${uri};foo=bar">path fragment</a>

where ${uri} basically translates to $_SERVER['REQUEST_URI'] in PHP, ${pageContext.request.requestURI} in JSP, and #{request.requestURI} in JSF. Noted should be that MVC frameworks like JSF have tags reducing all this boilerplate and removing the need for <base>. See also a.o. What URL to use to link / navigate to other JSF pages.

删除会话 2024-08-22 02:56:24

基本标签的效果细分:

基本标签似乎有一些非直观的效果,我建议在依赖 之前了解结果并亲自测试它们!因为我是在尝试使用基本标签来处理具有不同 url 的本地站点之后才发现它们的,并且只是在之后才发现了有问题的影响,令我沮丧的是,我觉得有必要创建这些潜在的总结给别人带来陷阱。

我将使用基本标签: 作为我在以下情况下的示例,并假装代码所在的页面是 http://localsite.com/original-subdirectory

主要:

否链接或命名锚点或空白 href 将指向原始子目录,除非明确说明:
基本标记使所有链接都不同,包括指向基本标记网址的同一页面锚链接,例如:

控制的链接上的这些问题,但是当您将依赖于标准的第三方库添加到混合中时行为,很容易酿成大祸。

次要:

需要条件注释的 IE6 修复:需要 ie6 的条件注释以避免搞乱 dom 层次结构,即 正如 BalusC 在上面的回答中提到的。

因此,总的来说,除非您对每个链接都有完全的编辑控制,否则主要问题使使用变得棘手,正如我最初担心的那样,这会带来更多麻烦,而不是值得的。现在我必须开始重写我对它的所有用法! :p

使用“fragments”/hashes 时测试问题的相关链接:

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

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


由 Izzy 编辑: 对于你们所有人,对于这些评论,都遇到了与我同样的困惑:

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

  • 尾随斜杠与否,与此处给出的示例没有区别(#anchor?query 将简单地附加到指定的)。
  • 然而,它对相对链接有所不同:省略尾部斜杠,other.htmldir/other.html 将从 DOCUMENT_ROOT 开始给定的示例,/other-subdirectory 被(正确地)视为文件,因此被省略。

因此,对于相对链接,BASE 可以很好地处理移动的页面 - 而锚点和 ?queries 需要显式指定文件名(使用 BASE有一个尾部斜杠,或者最后一个元素与它所使用的文件的名称不对应)。

将其视为 替换文件本身的完整 URL(而不是它所在的目录),并且您'会把事情做好的。假设本示例中使用的文件是 other-subdirectory/test.html(在移动到新位置之后),正确的规范应该是:


– 瞧,一切 em> 按预期工作:#anchor?queryother.htmlvery/other.html/complete/other.html

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, /other-subdirectory being (correctly) treated as file and thus omitted.

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.

感情旳空白 2024-08-22 02:56:24

好吧,等一下。我认为基本标签不值得这个坏名声。

基本标签的好处是它使您能够轻松地进行复杂的 URL 重写。

这是一个例子。您决定将 http://example.com/product/category/thisproduct 移至 http://example.com/product/thisproduct。您更改 .htaccess 文件以将第一个 URL 重写为第二个 URL。

基本标签就位后,您就可以重写 .htaccess 了。没问题。但如果没有基本标签,所有相关链接都会中断。

URL 重写通常是必要的,因为调整它们可以帮助您的网站架构和搜索引擎可见性。确实,您需要解决人们提到的“#”和“”问题。但基本标签值得在工具包中占有一席之地。

Well, wait a minute. I don't think the base tag deserves this bad reputation.

The nice thing about the base tag is that it enables you to do complex URL rewrites with less hassle.

Here's an example. You decide to move http://example.com/product/category/thisproduct to http://example.com/product/thisproduct. You change your .htaccess file to rewrite the first URL to the second URL.

With the base tag in place, you do your .htaccess rewrite and that's it. No problem. But without the base tag, all of your relative links will break.

URL rewrites are often necessary, because tweaking them can help your site's architecture and search engine visibility. True, you'll need workarounds for the "#" and '' problems that folks mentioned. But the base tag deserves a place in the toolkit.

青衫负雪 2024-08-22 02:56:24

要决定是否应该使用它,您应该了解它的作用以及是否需要它。 这个答案中已经部分概述了这一点,我也对此做出了贡献。但为了更容易理解和遵循,这里有第二个解释。首先我们需要了解:

在不使用 的情况下,浏览器如何处理链接?

对于一些示例,假设我们有以下 URL:

A) http://www.example.com/index.html
B) http://www.example.com/
C) http://www.example.com/page.html
D) http://www.example.com/subdir/page.html

A 和 B 都会生成相同的文件 (index.html) 发送到浏览器,C当然发送page.html,D发送/subdir/page.html

我们进一步假设,两个页面都包含以下类型的链接:

  1. 完全限定的绝对链接 (http://www...)
  2. 本地绝对链接 (/some/dir/page.html< /code>)
  3. 包括文件名 (dir/page.html) 的相对链接,以及
  4. 仅包含“段”的相对链接 (#anchor, ?foo=bar )。

浏览器接收页面并呈现 HTML。如果它找到某个 URL,它需要知道将其指向哪里。对于按原样采用的链接 1) 来说,这一点始终很明确。所有其他内容都取决于呈现页面的 URL:

URL     | Type | Result
--------+------+--------------------------
A,B,C,D |    2 | http://www.example.com/some/dir/page.html
A,B,C   |    3 | http://www.example.com/dir/page.html
D       |    3 | http://www.example.com/subdir/dir/page.html
A       |    4 | http://www.example.com/index.html#anchor
B       |    4 | http://www.example.com/#anchor
C       |    4 | http://www.example.com/page.html#anchor
D       |    4 | http://www.example.com/subdir/page.html#anchor

现在使用 会发生什么变化?

应该替换浏览器中显示的 URL。因此,它呈现所有链接,就好像用户调用了 中指定的 URL 一样。这解释了其他几个答案中的一些混乱:

  • 链接”,“完全限定的绝对链接”(“类型1”)没有任何变化。
  • 同样,对于“本地绝对 ,目标服务器可能会发生变化(如果 中指定的服务器与用户最初调用的服务器不同)
  • “相对 URL” 在这里变得至关重要,因此您必须特别注意如何设置
    • 最好避免将其设置为目录
      这样做,“类型 3”(相对目录 + 文件名)的链接可能会继续工作,但它肯定会破坏“类型 4”(相对 + 段)的链接;除了“case B”(没有路径或文件名)。
    • 将其设置为完全限定的文件名在大多数情况下会产生所需的结果。

一个例子可以很好地解释这一点

假设您想使用 mod_rewrite“美化”某些 URL:

  • real file: /some/dir/file.php?lang=en
  • 真实 URL:http://www.example.com/some/dir/file.php?lang=en
  • 用户友好 URL:http://www.example.com/en /file

让我们假设 mod_rewrite 用于透明地将用户友好的 URL 重写为真实的 URL(没有外部重定向,因此“用户友好的 URL” “其中一个保留在浏览器地址栏中,而真实的则被加载)。现在该怎么办?

  • 未指定:破坏所有相关链接(因为它们现在基于http://www.example.com/en/file

最后一点

请记住,这适用于文档中的所有 URL:

  • < ;SCRIPT SRC=

To decide whether it should be used or not, you should be aware of what it does and whether it's needed. This is already partly outlined in this answer, which I also contributed to. But to make it easier to understand and follow, a second explanation here. First we need to understand:

How are links processed by the browser without <BASE> being used?

For some examples, let's assume we have these URLs:

A) http://www.example.com/index.html
B) http://www.example.com/
C) http://www.example.com/page.html
D) http://www.example.com/subdir/page.html

A and B both result in the very same file (index.html) be sent to the browser, C of course sends page.html, and D sends /subdir/page.html.

Let's further assume, both pages contain links of the following types:

  1. fully qualified absolute links (http://www...)
  2. local absolute links (/some/dir/page.html)
  3. relative links including file names (dir/page.html), and
  4. relative links with "segments" only (#anchor, ?foo=bar).

The browser receives the page, and renders the HTML. If it finds some URL, it needs to know where to point it to. That's always clear for Link 1), which is taken as-is. All others depend on the URL of the rendered page:

URL     | Type | Result
--------+------+--------------------------
A,B,C,D |    2 | http://www.example.com/some/dir/page.html
A,B,C   |    3 | http://www.example.com/dir/page.html
D       |    3 | http://www.example.com/subdir/dir/page.html
A       |    4 | http://www.example.com/index.html#anchor
B       |    4 | http://www.example.com/#anchor
C       |    4 | http://www.example.com/page.html#anchor
D       |    4 | http://www.example.com/subdir/page.html#anchor

Now what changes with <BASE> being used?

<BASE> is supposed to replace the URL as it appears to the browser. So it renders all links as if the user had called up the URL specified in <BASE>. Which explains some of the confusion in several of the other answers:

  • again, nothing changes for "fully qualified absolute links" ("type 1")
  • for "local absolute links", the targeted server might change (if the one specified in <BASE> differs from the one being called initially from the user)
  • "relative URLs" become critical here, so you've got to take special care how you set <BASE>:
    • better avoid setting it to a directory.
      Doing so, links of "type 3" (relative dir + filename) might continue to work, but it most certainly breaks those of "type 4" (relative + segment); except for "case B" (no path or filename).
    • setting it to the fully qualified file name produces, in most cases, the desired results.

An example explains it best

Say you want to "prettify" some URL using mod_rewrite:

  • real file: <DOCUMENT_ROOT>/some/dir/file.php?lang=en
  • real URL: http://www.example.com/some/dir/file.php?lang=en
  • user-friendly URL: http://www.example.com/en/file

Let's assume mod_rewrite is used to transparently rewrite the user-friendly URL to the real one (no external re-direct, so the "user-friendly" one stays in the browsers address bar, while the real-one is loaded). What to do now?

  • no <BASE> specified: breaks all relative links (as they would be based on http://www.example.com/en/file now)
  • <BASE HREF='http://www.example.com/some/dir>: Absolutely wrong. dir would be considered the file part of the specified URL, so still, all relative links are broken.
  • <BASE HREF='http://www.example.com/some/dir/>: Better already. But relative links of "type 4" are still broken (except for "case B").
  • <BASE HREF='http://www.example.com/some/dir/file.php>: Exactly. Everything should be working with this one.

A last note

Keep in mind this applies to all URLs in your document:

  • <A HREF=
  • <IMG SRC=
  • <SCRIPT SRC=
薄情伤 2024-08-22 02:56:24

Drupal 最初依赖 标签,后来由于 HTTP 爬虫和浏览器的问题而决定不使用。缓存。

我一般不喜欢发布链接。但这确实值得分享,因为它可以使那些通过 标签寻找真实世界体验细节的人受益:

http://drupal.org/node/13148

Drupal initially relied on the <base> tag, and later on took the decision to not use due to problems with HTTP crawlers & caches.

I generally don't like to post links. But this one is really worth sharing as it could benefit those looking for the details of a real-world experience with the <base> tag:

http://drupal.org/node/13148

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文