<base> 是**文檔級全域配置標籤**,僅能出現在 <head> 中,**全文檔最多一個**。它定義兩件事:
href 屬性)target 屬性)| 屬性 | 必要性 | 說明 |
|---|---|---|
| href | 二選一必填 | 有效絕對 URL,作為文檔內所有相對 URL 的解析基準 |
| target | 二選一必填 | _blank、_self、_parent、_top 或具名視窗/iframe 名稱 |
<link>、<script>、<img>、<a>、<form> 等),<html manifest> 除外。href,僅第一個生效,其餘被忽略。<a href="#section"> 仍為文檔內部跳轉,不經 base 解析。base.href 不會重新解析既有相對 URL。<form action="submit.php"> 會以 base.href 解析 action。<!DOCTYPE html>
<html>
<head>
<title>Base 示範</title>
<base href="https://example.com/app/" target="_blank">
<link rel="stylesheet" href="assets/style.css"> <!-- 解析為 https://example.com/app/assets/style.css -->
</head>
<body>
<a href="page.html">跳轉</a> <!-- 實際導向 https://example.com/app/page.html,新分頁開啟 -->
<form action="api/save" method="post">...</form> <!-- 提交至 https://example.com/app/api/save -->
</body></html>
interface HTMLBaseElement : HTMLElement {
attribute DOMString href;
attribute DOMString target;
};
base 易干擾資源載入與 History API。rel="noopener noreferrer" 防止 window.opener 洩露。