Document.location

  • Revision slug: Web/API/document.location
  • Revision title: Document.location
  • Revision id: 467025
  • Created:
  • Creator: Mkmelin
  • Is current revision? No
  • Comment

Revision Content

{{ DomRef("Document") }}

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 load 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.

Syntax

locationObj = document.location
document.location = 'http://www.mozilla.org' // Equivalent to document.location.href = 'http://www.mozilla.org'

Example

dump(document.location); 
// Prints a string like
// "http://www.example.com/juicybits.html" to the console

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', "forms.html#dom-document-location", "Document.location")}} {{Spec2('HTML WHATWG')}} No change from {{SpecName("HTML5 W3C")}}.
{{SpecName('HTML5 W3C', "forms.html#dom-document-location", "Document.location")}} {{Spec2('HTML5 W3C')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

  • The interface of the returned value, {{domxref("Location")}}.
  • A similar information, but attached to the browsing context, {{domxref("Window.location")}}

 

Revision Source

<p>{{ DomRef("Document") }}</p>
<p>The <strong><code>Document.location</code></strong> 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 load another URL.</p>
<p>Though <code>Document.location</code> is a <em>read-only</em> <code>Location</code> 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: <code>document.location = 'http://www.example.com'</code> is a synonym of <code>document.location.href = 'http://www.example.com'</code>.</p>
<p>To retrieve just the URL as a string, the read-only {{domxref("document.URL")}} property can also be used.</p>
<p>If the current document is not in a browsing context, the returned value is <code>null</code>.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<em>locationObj</em> = document.location
document.location = 'http://www.mozilla.org' // Equivalent to document.location.href = 'http://www.mozilla.org'
</pre>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush: js">
dump(document.location); 
// Prints a string like
// "http://www.example.com/juicybits.html" to the console
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
    <tr>
      <td>{{SpecName('HTML WHATWG', "forms.html#dom-document-location", "Document.location")}}</td>
      <td>{{Spec2('HTML WHATWG')}}</td>
      <td>No change from {{SpecName("HTML5 W3C")}}.</td>
    </tr>
    <tr>
      <td>{{SpecName('HTML5 W3C', "forms.html#dom-document-location", "Document.location")}}</td>
      <td>{{Spec2('HTML5 W3C')}}</td>
      <td>Initial definition.</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li>The interface of the returned value, {{domxref("Location")}}.</li>
  <li>A similar information, but attached to the browsing context, {{domxref("Window.location")}}</li>
</ul>
<p>&nbsp;</p>
Revert to this revision