<base>: The Document Base URL element - HTML: HyperText Markup Language 编辑

The HTML <base> element specifies the base URL to use for all relative URLs in a document. There can be only one <base> element in a document.

A document's used base URL can be accessed by scripts with Node/baseURI. If the document has no <base> elements, then baseURI defaults to location.href.

Content categoriesMetadata content.
Permitted contentNone, it is an empty element.
Tag omissionThere must be no closing tag.
Permitted parentsA <head> that doesn't contain another <base> element.
Implicit ARIA roleNo corresponding role
Permitted ARIA rolesNo role permitted
DOM interfaceHTMLBaseElement

Attributes

This element's attributes include the global attributes.

If either of the following attributes are specified, this element must come before other elements with attribute values of URLs, such as <link>’s href attribute.

href
The base URL to be used throughout the document for relative URLs. Absolute and relative URLs are allowed.
target
A keyword or author-defined name of the default browsing context to show the results of navigation from <a>, <area>, or <form> elements without explicit target attributes.
The following keywords have special meanings:
  • _self (default): Show the result in the current browsing context.
  • _blank: Show the result in a new, unnamed browsing context.
  • _parent: Show the result in the parent browsing context of the current one, if the current page is inside a frame. If there is no parent, acts the same as _self.
  • _top: Show the result in the topmost browsing context (the browsing context that is an ancestor of the current one and has no parent). If there is no parent, acts the same as _self.

Usage notes

Multiple <base> elements

If multiple <base> elements are used, only the first href and first target are obeyed — all others are ignored.

In-page anchors

Links pointing to a fragment in the document — e.g. <a href="#some-id"> — are resolved with the <base>, triggering an HTTP request to the base URL with the fragment attached. For example:

  1. Given <base href="https://example.com">
  2. ...and this link: <a href="#anchor">Anker</a>
  3. ...the link points to https://example.com/#anchor.

Open Graph

Open Graph tags do not acknowledge <base>, and should always have full absolute URLs. For example:

<meta property="og:image" content="https://example.com/thumbnail.jpg">

Examples

<base href="https://www.example.com/">
<base target="_blank">
<base target="_top" href="https://example.com/">

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of '<base>' in that specification.
Living StandardNo change since last snapshot.
HTML5
The definition of '<base>' in that specification.
RecommendationSpecified the behavior of target
HTML 4.01 Specification
The definition of '<base>' in that specification.
RecommendationAdded the target attribute

Browser compatibility

BCD tables only load in the browser

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:123 次

字数:6782

最后编辑:7年前

编辑次数:0 次

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