HTML DOM
HTML DOM URL Property
The URL property returns the complete URL of the document as a string. It is a read-only way to find out the exact address of the page currently loaded in the browser.
Definition and Usage
document.URL returns the full URL of the current document, including the protocol, host, path, and any query string. It is read-only, so you cannot navigate by assigning to it - use window.location.href for that.
It is closely related to window.location.href, and in most cases the two return the same value. document.URL is handy for logging, analytics, or building absolute links back to the current page.
Syntax
document.URLExample
Click the button to display the current document's full URL in the output box.
<!DOCTYPE html>
<html>
<body>
<button onclick="showUrl()">Show page URL</button>
<p id="out"></p>
<script>
function showUrl() {
document.getElementById("out").textContent =
"This page's URL is: " + document.URL;
}
</script>
</body>
</html>More Examples
This demo compares document.URL with window.location.href to show they normally match.
<!DOCTYPE html>
<html>
<body>
<p id="out"></p>
<script>
const same = document.URL === window.location.href;
document.getElementById("out").textContent =
"document.URL matches location.href: " + same;
</script>
</body>
</html>document.URL is read-only. To change the page location, assign to window.location.href instead.
Key Takeaways
- document.URL returns the full URL of the current page as a string.
- It is read-only and cannot be used to navigate.
- It usually matches window.location.href.
- Useful for logging, analytics, and building absolute links.
