Return the hostname of a link:
Click the button to display the hostname of the link.
var x = document.getElementById("myAnchor").hostname;
<!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.