--- title: Document.location slug: Web/API/Document/location tags: - API - Document - HTML DOM - Property - Read-only - Reference translation_of: Web/API/Document/location ---
{{APIRef("DOM")}}
The Document.location
read-only property returns a {{domxref("Location")}} object, which contains information about the URL of the document and provides methods for changing that URL and loading another URL.
Though Document.location
is a read-only Location
object, you can also assign a {{domxref("DOMString")}} to it. This means that you can work with document.location as if it were a string in most cases: document.location = 'http://www.example.com'
is a synonym of document.location.href = 'http://www.example.com'
.
To retrieve just the URL as a string, the read-only {{domxref("document.URL")}} property can also be used.
If the current document is not in a browsing context, the returned value is null
.
locationObj = document.location document.location = 'http://www.mozilla.org' // Equivalent to document.location.href = 'http://www.mozilla.org'
dump(document.location); // Prints a string like // "http://www.example.com/juicybits.html" to the console
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "history.html#the-location-interface", "Document.location")}} | {{Spec2('HTML WHATWG')}} | No change from {{SpecName("HTML5 W3C")}}. |
{{SpecName('HTML5 W3C', "browsers.html#the-location-interface", "Document.location")}} | {{Spec2('HTML5 W3C')}} | Initial definition. |
{{Compat("api.Document.location")}}