Javascript Browser Location hostname Property get

Introduction

Return the hostname of the current URL:

var x = location.hostname;

Click the button to display the hostname of the current URL.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*from   ww  w. j a v  a  2s. co  m*/
  var x = location.hostname;
  document.getElementById("demo").innerHTML= x;
}
</script>

</body>
</html>

The hostname property sets or gets the hostname of a URL.

Property Values

ValueType Description
hostname String Specifies the hostname of the URL

The hostname property returns a String representing the domain name, or the IP address of a URL.




PreviousNext

Related