I want the hostname of my website using JavaScript. Do you know how to get hostname? Which parameter or function retrieves the hostname?
Printable View
I want the hostname of my website using JavaScript. Do you know how to get hostname? Which parameter or function retrieves the hostname?
Try using nametop.location.host, something like this:
HTML Code:<script language = "JavaScript">
function name ()
{
alert (nametop.location.host);
}
</script>
To get host name in JavaScript addthis statement in your code.Code:var hostname = window.location.hostname
However if you want you can use "VIH_HostName" variable. The client host name is stored in this variable.
Here is a simple example how to get the hostname of your web site. Basically the hostname property of Location class represents the hostname portion of the URL.
HTML Code:<html>
<head>
<title> Using the hostname property of the Location object</title>
</head>
<body>
<script language="JavaScript">
<!--
function show(){
alert(document.location.hostname);
}
-->
</script>
<form name="form1">
Click the button to get the current location.hostname
<br>
http://www.java2s.com
<br>
<input type="button" name="gethost" value="Get hostname" onClick='show()'>
<br>
</form>
</body>
</html>