JavaScript Document URL Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.URL is a read-only instance property that returns the document location as a string—the address of the current page. Learn how it compares to document.documentURI and location.href, how to display it in the UI, parse URL parts, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

URL string

03

Same as

documentURI

04

Related

location.href

05

Navigate?

Use location

06

Status

Baseline widely

Introduction

The browser address bar shows where you are on the web. JavaScript can read that same address from the active document through document.URL.

MDN: the URL read-only property of the Document interface returns the document location as a string. MDN also notes that document.documentURI returns the same value.

💡
Read, don’t write

document.URL tells you where the document lives. To go somewhere else, use location.href, location.assign(), or History APIs—not an assignment to document.URL.

Related Document tutorials: documentURI, referrer, title, Document constructor.

Understanding Document.URL

A read-only instance property on Document. Its value is a string naming the document’s full location.

  • Type — string (MDN).
  • Read-only — you cannot assign a new URL here.
  • Same as document.documentURI — MDN documents matching values.
  • Typical content — scheme, host, path, query, and hash when present.
  • Status — Baseline Widely available (since July 2015, MDN).

📝 Syntax

JavaScript
document.URL

Value

A string containing the URL of the document (MDN).

MDN-style display

JavaScript
document.getElementById("url").textContent = document.URL;

⚡ Quick Reference

GoalCode / note
Read document addressdocument.URL
Same valuedocument.documentURI
Show in the pageel.textContent = document.URL
Compare with locationdocument.URL === location.href
Change addresslocation.href = "…" (not document.URL)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.URL.

Type
string

Location

Access
read-only

No setter

Alias
documentURI

Same value

Status
baseline

Since 2015

📋 What the string usually contains

PartExampleIn document.URL?
Origin + pathhttps://example.com/pageYes
Query string?q=1Yes (when present)
Hash / fragment#sectionYes (when present)
Path onlylocation.pathnameUse Location for pieces

Examples Gallery

Examples follow MDN Document: URL. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read the location string and display it (MDN).

Example 1 — Log document.URL

Print the current document address (MDN).

JavaScript
console.log(document.URL);
// e.g. "https://www.codetofun.com/js/document/properties/url"
Try It Yourself

How It Works

The browser reports the address associated with this Document object.

Example 2 — Show the URL in the Page (MDN)

MDN’s pattern: write document.URL into a text node.

HTML + JS
<p id="urlText">
  URL:<br />
  <span id="url">URL goes here</span>
</p>
JavaScript
document.getElementById("url").textContent = document.URL;
Try It Yourself

How It Works

Useful for debug panels, share links, or “you are here” indicators in admin UIs.

📈 documentURI, href & Parsing

Compare related APIs and break the string into parts.

Example 3 — Same as document.documentURI (MDN)

Confirm both Document properties match.

JavaScript
console.log(document.URL);
console.log(document.documentURI);
console.log(document.URL === document.documentURI); // true
Try It Yourself

How It Works

MDN: document.documentURI returns the same value as document.URL.

Example 4 — Compare with location.href

Location is the usual API when you also need to navigate.

JavaScript
console.log("document.URL:", document.URL);
console.log("location.href:", location.href);
console.log("Equal:", document.URL === location.href);
Try It Yourself

How It Works

On the active page they usually match. Prefer location when assigning a new address or reading pathname/search/hash.

Example 5 — Parse Pieces with the URL Constructor

Turn the string into structured parts without manual string splitting.

JavaScript
const u = new URL(document.URL);

console.log("origin:", u.origin);
console.log("pathname:", u.pathname);
console.log("search:", u.search);
console.log("hash:", u.hash);
Try It Yourself

How It Works

Or read location.pathname, search, and hash directly when you already have the Location object.

🚀 Common Use Cases

  • Debug panels — show the live document address (MDN demo style).
  • Logging / analytics — record which page URL produced an event.
  • Share / copy link — seed a clipboard helper from document.URL.
  • Multi-document tools — when you hold a Document, read its URL without assuming global location.
  • Canonical checks — compare the live URL with expected routes in SPAs.
  • Not for navigation — assign location.href or use History instead.

🧠 How document.URL Works

1

Browser loads a location

User opens a URL or a script navigates.

Load
2

Document stores the address

The Document object knows its location string (MDN).

Document
3

Scripts read document.URL

Same value as document.documentURI; often matches location.href.

Read
4

To change address, use Location

location.href, assign, replace, or History APIs—not document.URL.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • document.URL and document.documentURI return the same value (MDN).
  • Read-only on Document — use location to navigate.
  • On iframes, each frame’s contentDocument.URL reflects that frame’s address (when accessible).
  • Related: documentURI, referrer, title.

Browser Support

Document.URL is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.URL

Read-only string — full document location (same value as documentURI).

Widely Available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported (legacy)
Legacy support
Document.URL Baseline support

Bottom line: Use document.URL to read the page address from a Document. Pair with location or the URL constructor when you need navigation or parsed parts.

Conclusion

Document.URL is a read-only string that tells you where the current document lives. It matches document.documentURI and usually equals location.href on the active page. Use it to display, log, or parse the address—but use location when you need to navigate.

Continue with visibilityState, ownerDocument, documentURI, referrer, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use document.URL to read the page address from Document
  • Compare with document.documentURI when reviewing legacy code
  • Parse with new URL(document.URL) for structured parts
  • Use location when you need to navigate or assign href
  • Log URLs in analytics for reproducible debugging

❌ Don’t

  • Assign to document.URL (read-only)
  • Assume it differs from document.documentURI (MDN: same value)
  • Hand-parse URLs with regex when the URL API exists
  • Confuse document.URL with document.baseURI
  • Read cross-origin iframe URLs without expecting security errors

Key Takeaways

Knowledge Unlocked

Five things to remember about document.URL

Read-only document location string.

5
Core concepts
📄02

Value

Location

MDN
🔁03

Alias

documentURI

Same
🚀04

Navigate

location

Not URL
05

Status

Baseline

2015+

❓ Frequently Asked Questions

A read-only string with the document location — the full address of the current page (MDN). It is the URL you would use to open this document again.
No. MDN marks Document.URL as Baseline Widely available (since July 2015). It is a standard read-only Document instance property.
MDN states they return the same value. document.URL is the familiar name many developers use; documentURI is the standards-oriented alias on Document.
No. It is read-only on Document. To navigate, use location.href, location.assign(), location.replace(), or History APIs.
Both often return the same string on the active page. Use location when you also need to navigate or read pathname, search, and hash. Use document.URL when you have a Document reference and only need the location string.
Yes — it is the full location string, typically including path, query (?key=value), and fragment (#section) when present.
Did you know?

MDN lists both document.URL and document.documentURI as ways to get the document location, and explicitly states they return the same value. Many codebases use document.URL because the name matches everyday speech—“what’s the page URL?”

Next: visibilityState

Learn the Page Visibility API string — visible or hidden tab state.

visibilityState →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful