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
Fundamentals
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.
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 = "...".
Applications
🚀 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 users — location.assign() after login or form submit.
Replace history entry — location.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).
Important
📝 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).
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in legacy IE
Full support
Document.locationExcellent
Bottom line: Use document.location (or window.location) to read URL parts and navigate. Use document.URL when you only need the URL string.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.location
Your Document handle to the page URL.
5
Core concepts
🌐01
Returns
Location
API
🔗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.