Return the base URL for all relative URLs on a page.
var x = document.getElementById("myBase").href;
Click the button to return the value of the href attribute of the base element.
<!DOCTYPE html> <html> <head> <base id="myBase" href="https://www.java2s.com/"> </head>/*from w w w . j a v a 2 s . co m*/ <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myBase").href; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
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://;