Javascript DOM HTML Base href Property set

Introduction

The href property sets or gets the href attribute in a base element.

The href attribute sets a base URL for all relative URLs on a page.

By default, the base URL is the location of the current document.

It can be overridden by this property.

The href property returns the base URL for all relative URLs on a page, including the protocol, for example http://;

Change the value of the base URL:

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

Click the button to change the value of the href attribute of the base element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<base id="myBase" href="/jsref/">
</head>/*from   w w w. ja  va 2 s  .c om*/
<body>

<a href="default.asp">Default</a>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("myBase").href = "/html/";
  document.getElementById("demo").innerHTML = "Base URL was changed from /jsref/ to /html/.";
}
</script>

</body>
</html>



PreviousNext

Related