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:
<html>
<body>
<script type="text/javascript">
document.write(location.href);
</script>
</body>
</html>
This code gives us a page with this text:
http://www.devshed.com/jamespayneisawesome.html
Next: Getting the Pathname >>
More JavaScript Articles
More By James Payne