In this article we will cover the DOM Location Objects, which are used to set or return information regarding a user’s URL. Often considered an HTML DOM object, it is really more of a JavaScript object, created by the runtime engine. Like other objects, it consists of both Properties and Methods, the former of which we will be discussing today.
The DOM Location Object Properties - Using Href (Page 4 of 6 )
The href object is used to retrieve or set the entire URL, unlike the host/hostname objects, which only return the host part. Using the same sample URL as in the previous two examples, we are going to create an alert box to display the current full URL. Here is the code:
<html>
<body>
<script type="text/javascript">
alert(location.href);
</script>
</body>
</html>
This will give us a pop-up alert containing the text:
http://www.devshed.com/jamespayneisawesome.html
Likewise, we could create a page that displays the URL on the page instead of in an alert box in this simple fashion: