blob: 96c514e4b9b0e2a365be9a04f0ab32fae1cef3d0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
---
title: Document.location
slug: Web/API/Document/location
tags:
- API
- Document
- HTML DOM
- Property
- Read-only
- Reference
translation_of: Web/API/Document/location
---
<p>{{APIRef("DOM")}}</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 loading 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', "history.html#the-location-interface", "Document.location")}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td>No change from {{SpecName("HTML5 W3C")}}.</td>
</tr>
<tr>
<td>{{SpecName('HTML5 W3C', "browsers.html#the-location-interface", "Document.location")}}</td>
<td>{{Spec2('HTML5 W3C')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Document.location")}}</p>
<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 {{Glossary("browsing context")}}, {{domxref("Window.location")}}.</li>
</ul>
|