The DOM Location Object Properties - Being a Good Host
(Page 3 of 6 )
The host object sets or retrieves the hostname and portnumber (in the following example you may or may not see a portnumber; if your portnumber is the default of 80, like mine, you will not see it; otherwise you will). Its usage is pretty basic, and the best way to explain it is to use it in an example. So without further ado, here it is in all its simplistic glory:
<html>
<body>
<script type="text/javascript">
alert(location.host);
</script>
</body>
</html>
If we used this code on a page such as http://www.devshed.com/jamespayneisawesome.html, then our result would be:
www.devshed.com
It's just that simple folks.
Hostname
Hostname, as far as I can see, is pretty much the same as host, except it returns just the hostname and not the portnumber. We use it in the same manner. Here is the code:
<html>
<body>
<script type="text/javascript">
alert(location.hostname);
</script>
</body>
</html>
And the result?
www.devshed.com
Next: Using Href >>
More JavaScript Articles
More By James Payne