Javascript DOM HTML Anchor hostname Property get

Introduction

Return the hostname of a link:

Click the button to display the hostname of the link.

var x = document.getElementById("myAnchor").hostname;

View in separate window

<!DOCTYPE html>
<html>
<body>

<p><a id="myAnchor" href="http://www.java2s.com:80/">Example link</a></p>

<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {//from  w ww . jav a 2s  .c om
  var x = document.getElementById("myAnchor").hostname;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The hostname property sets or gets the host name part of the href attribute value.




PreviousNext

Related