Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 987769 of Location

  • Revision slug: Web/API/Location
  • Revision title: Location
  • Revision id: 987769
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Precise that setter will navigate away from the page.

Revision Content

{{APIRef}}

The Location interface represents the location of the object it is linked to. Changes done on it are reflected on the object it relates to. Both the {{domxref("Document")}} and {{domxref("Window")}} interface have such a linked Location, accessible via {{domxref("Document.location")}} and {{domxref("Window.location")}} respectively.

Properties

The Location interface doesn't inherit any property, but implements those from {{domxref("URLUtils")}}.

{{domxref("Location.href")}}
Is a {{domxref("DOMString")}} containing the whole URL. If changed the associated document navigate to the new page. It can be set from a different origin than the associated document.
{{domxref("Location.protocol")}}
Is a {{domxref("DOMString")}} containing the protocol scheme of the URL, including the final ':'.
{{domxref("Location.host")}}
Is a {{domxref("DOMString")}} containing the host, that is the hostname, a ':', and the port of the URL.
{{domxref("Location.hostname")}}
Is a {{domxref("DOMString")}} containing the domain of the URL.
{{domxref("Location.port")}}
Is a {{domxref("DOMString")}} containing the port number of the URL.
{{domxref("Location.pathname")}}
Is a {{domxref("DOMString")}} containing an initial '/' followed by the path of the URL.
{{domxref("Location.search")}}
Is a {{domxref("DOMString")}} containing a '?' followed by the parameters of the URL. Also known as "querystring".
{{domxref("Location.hash")}}
Is a {{domxref("DOMString")}} containing a '#' followed by the fragment identifier of the URL.
{{domxref("Location.username")}}
Is a {{domxref("DOMString")}} containing the username specified before the domain name.
{{domxref("Location.password")}}
Is a {{domxref("DOMString")}} containing the password specified before the domain name.
{{domxref("Location.origin")}} {{readOnlyInline}}
Returns a {{domxref("DOMString")}} containing the canonical form of the origin of the specific location.

Methods

The Location interface doesn't inherit any method, but implements those from {{domxref("URLUtils")}}.

{{domxref("Location.assign()")}}
Loads the resource at the URL provided in parameter.
{{domxref("Location.reload()")}}
Reloads the resource from the current URL. Its optional unique parameter is a {{domxref("Boolean")}}, which, when it is true, causes the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache.
{{domxref("Location.replace()")}}
Replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session {{domxref("History")}}, meaning the user won't be able to use the back button to navigate to it.
{{domxref("Location.toString()")}}
Returns a {{domxref("DOMString")}} containing the whole URL. It is a synonym for {{domxref("URLUtils.href")}}, though it can't be used to modify the value.

Examples

// Create anchor element and use href property for the purpose of this example
// A more correct alternative is to browse to the URL and use document.location or window.location
var url = document.createElement('a');
url.href = 'https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container';
console.log(url.href);      // https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container
console.log(url.protocol);  // https:
console.log(url.host);      // developer.mozilla.org
console.log(url.hostname);  // developer.mozilla.org
console.log(url.port);      // (blank - https assumes port 443)
console.log(url.pathname);  // /en-US/search
console.log(url.search);    // ?q=URL
console.log(url.hash);      // #search-results-close-container
console.log(url.origin);    // https://developer.mozilla.org

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', "history.html#the-location-interface", "Location")}} {{Spec2('HTML WHATWG')}} No change from {{SpecName("HTML5 W3C")}}.
{{SpecName('HTML5 W3C', "browsers.html#the-location-interface", "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}}
origin on Windows.location {{CompatUnknown}} {{CompatGeckoDesktop("21")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
origin on all location objects (but on Workers, that use {{domxref("WorkerLocation")}} {{CompatUnknown}} {{CompatGeckoDesktop("26")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
username and password {{CompatUnknown}} {{CompatGeckoDesktop("26")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
origin on Windows.location {{CompatUnknown}} {{CompatGeckoMobile("21")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
origin on all location objects (but on Workers, that use {{domxref("WorkerLocation")}}) {{CompatUnknown}} {{CompatGeckoMobile("26")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
username and password {{CompatUnknown}} {{CompatGeckoMobile("26")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

  • Two methods creating such an object: {{domxref("Window.location")}} and {{domxref("Document.location")}}.

Revision Source

<p>{{APIRef}}</p>

<p>The <strong><code>Location</code></strong> interface represents the location of the object it is linked to. Changes done on it are reflected on the object it relates to. Both the {{domxref("Document")}} and {{domxref("Window")}} interface have such a linked <code>Location</code>, accessible via {{domxref("Document.location")}} and {{domxref("Window.location")}} respectively.</p>

<h2 id="Properties">Properties</h2>

<p><em>The <code>Location</code></em><em> interface doesn't inherit any property, but implements those from {{domxref("URLUtils")}}.</em></p>

<dl>
 <dt>{{domxref("Location.href")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing the whole URL. If changed the associated document navigate to the new page. It can be set from a different origin than the associated document.</dd>
 <dt>{{domxref("Location.protocol")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing the protocol scheme of the URL, including the final <code>':'</code>.</dd>
 <dt>{{domxref("Location.host")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing the host, that is the <em>hostname</em>, a <code>':'</code>, and the <em>port</em> of the URL.</dd>
 <dt>{{domxref("Location.hostname")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing the domain of the URL.</dd>
 <dt>{{domxref("Location.port")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing the port number of the URL.</dd>
 <dt>{{domxref("Location.pathname")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing an initial <code>'/'</code> followed by the path of the URL.</dd>
 <dt>{{domxref("Location.search")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing a <code>'?'</code> followed by the parameters of the URL. Also known as "querystring".</dd>
 <!-- <dt>
  {{domxref("URLUtils.searchParams")}}</dt>
 <dd>
  Returns a {{domxref("URLSearchParams")}} object allowing to access the GET query arguments contained in the URL.</dd> -->
 <dt>{{domxref("Location.hash")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing a <code>'#'</code> followed by the fragment identifier of the URL.</dd>
 <dt>{{domxref("Location.username")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing the username specified before the domain name.</dd>
 <dt>{{domxref("Location.password")}}</dt>
 <dd>Is a {{domxref("DOMString")}} containing the password specified before the domain name.</dd>
 <dt>{{domxref("Location.origin")}} {{readOnlyInline}}</dt>
 <dd>Returns a {{domxref("DOMString")}} containing the canonical form of the origin of the specific location.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<p><em>The <code>Location</code></em><em> interface doesn't inherit any method<em>, but implements those from {{domxref("URLUtils")}}</em>.</em></p>

<dl>
 <dt>{{domxref("Location.assign()")}}</dt>
 <dd>Loads the resource at the URL provided in parameter.</dd>
 <dt>{{domxref("Location.reload()")}}</dt>
 <dd>Reloads the resource from the current URL. Its optional unique parameter is a {{domxref("Boolean")}}, which, when it is <code>true</code>, causes the page to always be reloaded from the server. If it is <code>false</code> or not specified, the browser may reload the page from its cache.</dd>
 <dt>{{domxref("Location.replace()")}}</dt>
 <dd>Replaces the current resource with the one at the provided URL. The difference from the <code>assign()</code> method is that after using <code>replace()</code> the current page will not be saved in session {{domxref("History")}}, meaning the user won't be able to use the <em>back</em> button to navigate to it.</dd>
 <dt>{{domxref("Location.toString()")}}</dt>
 <dd>Returns a {{domxref("DOMString")}} containing the whole URL. It is a synonym for {{domxref("URLUtils.href")}}, though it can't be used to modify the value.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<pre class="brush: js">
// Create anchor element and use href property for the purpose of this example
// A more correct alternative is to browse to the URL and use document.location or window.location
var url = document.createElement('a');
url.href = 'https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container';
console.log(url.href);      // https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container
console.log(url.protocol);  // https:
console.log(url.host);      // developer.mozilla.org
console.log(url.hostname);  // developer.mozilla.org
console.log(url.port);      // (blank - https assumes port 443)
console.log(url.pathname);  // /en-US/search
console.log(url.search);    // ?q=URL
console.log(url.hash);      // #search-results-close-container
console.log(url.origin);    // https://developer.mozilla.org
</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", "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", "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>
  <tr>
   <td><code>origin</code> on <code>Windows.location</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("21")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>origin</code> on all <code>location</code> objects (but on Workers, that use {{domxref("WorkerLocation")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("26")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>username</code> and <code>password</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("26")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <!-- <tr>
    <td><code>searchParams</code></td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatGeckoDesktop("34")}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</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>
  <tr>
   <td><code>origin</code> on <code>Windows.location</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("21")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>origin</code> on all <code>location</code> objects (but on Workers, that use {{domxref("WorkerLocation")}})</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("26")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>username</code> and <code>password</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("26")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <!-- <tr>
    <td><code>searchParams</code></td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatGeckoMobile("34")}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr> -->
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>Two methods creating such an object: {{domxref("Window.location")}} and {{domxref("Document.location")}}.</li>
</ul>
Revert to this revision