JavaScript Document location Property

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

What You’ll Learn

Document.location is a read-only instance property that returns a Location object describing the document’s URL. Learn MDN’s basics, how it relates to window.location and document.URL, URL parts like href and pathname, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

Location

03

Read

href, hash

04

Navigate

assign / href

05

Same as

window.location

06

Status

Baseline widely

Introduction

Every web page has an address in the browser’s address bar. JavaScript exposes that address through the Location interface. On a normal page, document.location is your Document-side handle to that URL.

MDN: the read-only property returns a Location object, which contains information about the URL of the document and provides methods for changing that URL and loading another URL.

💡
Need just the URL string?

Use the read-only document.URL (or document.documentURI) when you only want a string. Use document.location when you also need URL segments or navigation methods.

Related Document tutorials: documentURI, domain, links, Document constructor.

Understanding Document.location

A read-only instance property on Document. Its value is a Location object tied to the document’s browsing context.

  • Value — a Location object (MDN); null if the document is not in a browsing context.
  • Read URL partshref, protocol, host, pathname, search, hash, origin.
  • Navigatelocation.assign(url), location.replace(url), or assign location.href.
  • Direct assignmentdocument.location = url is equivalent to setting href (MDN).
  • Window twindocument.location === window.location on a typical page.

📝 Syntax

JavaScript
document.location

Value

A Location object — or null when the document has no browsing context (MDN).

MDN example

JavaScript
console.log(document.location);
// Prints a Location object to the console

Read URL segments (Location interface)

JavaScript
const loc = document.location;
console.log(loc.href);      // full URL
console.log(loc.pathname);  // path after host
console.log(loc.search);    // ?query=string
console.log(loc.hash);      // #fragment

⚡ Quick Reference

GoalCode / note
Location objectdocument.location
Full URLdocument.location.href
Path onlydocument.location.pathname
Query stringdocument.location.search
Fragmentdocument.location.hash
Navigatedocument.location.assign(url)
MDN statusBaseline Widely available (since Jul 2015)

🔍 At a Glance

Four facts about document.location.

Type
Location

Object

Property
read-only

On Document

Navigate
href / assign

Loads URLs

Status
baseline

Widely available

📋 document.location vs document.URL

document.locationdocument.URL
TypeLocation objectString
Read-only on Document?Property is read-only; Location fields can be setYes (string only)
Navigation methodsassign, replace, reloadNo
Best forURL parts + go to new pageLog current URL string

Examples Gallery

Examples follow MDN Document: location and the Location interface. Try-it labs read URL data safely without leaving the tutorial page.

📚 Getting Started

Read the Location object and its URL segments.

Example 1 — Read document.location (MDN)

Log the Location object attached to the current document.

JavaScript
console.log(document.location);
// Location { ... } in DevTools
console.log(typeof document.location); // "object"
Try It Yourself

How It Works

The console shows a live Location object, not a plain string. Expand it in DevTools to see href, pathname, and more.

Example 2 — Read URL Parts

Break the current address into protocol, host, path, query, and hash (MDN Location properties).

JavaScript
const loc = document.location;
console.log(loc.href);
console.log(loc.protocol);
console.log(loc.hostname);
console.log(loc.pathname);
console.log(loc.search);
console.log(loc.hash);
Try It Yourself

How It Works

Each property maps to a segment of the URL. Together they reconstruct the full href.

📈 Compare & Update Safely

Related APIs and hash-only navigation for in-page jumps.

Example 3 — Same Object as window.location

On a normal page, Document and Window share one Location object.

JavaScript
console.log(document.location === window.location);
// true on a typical web page
Try It Yourself

How It Works

MDN lists both Document.location and Window.location. Most developers write window.location out of habit, but both work the same way here.

Example 4 — Compare with document.URL

MDN: for just the URL string, document.URL also works.

JavaScript
console.log(document.location.href);
console.log(document.URL);
console.log(document.location.href === document.URL);
// Usually true on the same page
Try It Yourself

How It Works

document.URL is read-only on Document. Use it when you only need a string; use location when you need parts or navigation.

Example 5 — Change the Hash (In-Page Jump)

Setting location.hash updates the URL fragment without a full page reload.

JavaScript
console.log("Before:", document.location.hash);
document.location.hash = "#section-b";
console.log("After:", document.location.hash);
// Address bar shows #section-b; page does not reload
Try It Yourself

How It Works

Hash changes are a gentle form of navigation—great for table-of-contents links and SPA-style routing. Full redirects use location.assign() or location.href = "...".

🚀 Common Use Cases

  • Read current URL — log or copy document.location.href.
  • Route detection — branch on pathname or search params.
  • In-page anchors — set location.hash for section jumps.
  • Redirect userslocation.assign() after login or form submit.
  • Replace history entrylocation.replace() when back button should skip a step.
  • Analytics — send origin + pathname to tracking scripts.

🧠 How document.location Works

1

Browser loads a document

The page enters a browsing context with an address (URL).

Load
2

Location object is linked

document.location exposes the shared Location for that context.

Link
3

You read or mutate URL fields

Reading href is safe; writing href or calling assign navigates.

Access
4

Browser updates the address bar

Changes to Location reflect in the UI and may load a new document (except some hash-only updates).

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • You cannot replace the Location object; you only read the property or mutate Location fields.
  • document.location = url navigates the same way as location.href = url (MDN).
  • Outside a browsing context, document.location may be null (MDN).
  • Related: documentURI, domain, links, Document constructor.

Universal Browser Support

Document.location 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.location

Read-only Location object for the document URL — widely supported navigation API.

Universal 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 in legacy IE
Full support
Document.location Excellent

Bottom line: Use document.location (or window.location) to read URL parts and navigate. Use document.URL when you only need the URL string.

Conclusion

Document.location connects your JavaScript to the page URL through a standard Location object. Read href and its parts, compare with document.URL, and use assign or hash updates when you need to navigate.

Continue with pictureInPictureElement, ownerDocument, documentURI, links, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use location.pathname and search for route logic
  • Prefer URLSearchParams to parse query strings
  • Use location.hash for in-page section jumps
  • Use location.replace() when back button should skip a page
  • Remember document.location === window.location on normal pages

❌ Don’t

  • Assign full URLs from untrusted user input without validation
  • Confuse read-only document.URL with mutable Location fields
  • Expect document.location when the document has no browsing context
  • Rely on location.reload() in loops (bad UX)
  • Hard-code domains when location.origin is available

Key Takeaways

Knowledge Unlocked

Five things to remember about document.location

Your Document handle to the page URL.

5
Core concepts
🔗02

href

full URL

Read
🚀03

Navigate

assign

Go
🔄04

Same as

window.location

Twin
📄05

String?

document.URL

Alt

❓ Frequently Asked Questions

A Location object with information about the document URL and methods to change it (such as assign and replace). If the document is not in a browsing context, MDN says the value is null.
No. MDN marks Document.location as Baseline Widely available (since July 2015). It is a standard Document instance property.
On a normal page they refer to the same Location object for that browsing context. Document.location is on the Document; Window.location is on the Window. Both expose href, pathname, hash, and navigation methods.
Yes. Although you cannot replace the Location object itself, assigning a URL string to document.location is equivalent to assigning to location.href and navigates the page (MDN).
Use document.URL when you only need the current page URL as a read-only string. Use document.location when you also need URL parts (pathname, search, hash) or navigation methods.
Both load a new URL. location.replace() does not keep the current page in session history, so the user cannot use the back button to return. location.assign() (or setting href) does keep history.
Did you know?

Assigning document.location = "https://example.com" looks unusual but is valid—MDN treats it like setting location.href. Many teams still write window.location.href = ... for clarity, even though document.location points at the same object.

Next: pictureInPictureElement

Learn which element is playing in picture-in-picture mode.

pictureInPictureElement →

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