Javascript Reference - HTML DOM Base Object








The Base object represents an HTML <base> element.

Base Object Properties

Property Description
href Sets or gets the value of the base element's href attribute
target Sets or gets the value of the base element's target attribute

Standard Properties and Events

The Base object supports the standard properties and events.

Example

The following code shows how to get a <base> element by using getElementById().


<!DOCTYPE html>
<html>
<head><base id="myBase" href="http://www.java2s.com/new/"></head>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from   w  w  w  .ja  va2 s.  com-->
    var x = document.getElementById("myBase").href;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to create a <base> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<head>
</head><!--   ww  w.  ja va  2 s.  c  o m-->
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.createElement("BASE");
    x.setAttribute("href", "http://www.java2s.com");
    document.head.appendChild(x);
    document.getElementById("demo").innerHTML = "changed";
}
</script>
</body>
</html>

The code above is rendered as follows: