Javascript Browser Location host Property get

Introduction

Return the hostname and port of the current URL:

var x = location.host;

Click the button to display the hostname and port number 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   w w  w . j a  v a2  s  .co  m
  var x = location.host;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The host property sets or gets the hostname and port of a URL.

If the port number is not specified in the URL (or if it is the default port - like 80, or 443), some browsers will not display the port number.

Property Values

Value Type Description
hostname:port String Specifies the hostname and port number of the URL

The host property returns a String representing the domain name and port number, or the IP address of a URL.




PreviousNext

Related