Base href attribute

Definition and usage

href Sets or returns the value of the href attribute of the base element.

Base href attribute Specify the base URL for all relative URLs on the page.

By default, the base URL is the location of the current document, but it can be overridden by this attribute.

See also:

HTML 参考手册:HTML <base> 标签

Example

Example 1

Return the base URL for all relative URLs on the page:

var x = document.getElementById("myBase").href;

Try it yourself

Example 2

Change the value of the base URL:

document.getElementById("myBase").href = "https://www.codew3c.com/html/";

Try it yourself

Syntax

Return href attribute:

baseObject.href

Set href attribute:

baseObject.href = URL

属性值

描述
URL 规定基准 URL。

技术细节

返回值: 字符串值,表示页面上所有相对 URL 的基准 URL,包括协议(如 http://)。

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持

相关页面

HTML 参考手册:HTML <base> href 属性