Location hostname Property - Javascript Browser Object Model

Javascript examples for Browser Object Model:Location


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

Set the hostname property with the following Values

ValueType Description
hostname String Sets the hostname of the URL

Return Value

A String, representing the domain name, or the IP address of a URL

The following code shows how to return the hostname of the current URL:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

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

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

function myFunction() {/*from  ww  w.  j av a 2s  .  c o  m*/
    var x = location.hostname;
    document.getElementById("demo").innerHTML= x;


Related Tutorials