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 1031512 of URLSearchParams

  • Revision slug: Web/API/URLSearchParams
  • Revision title: URLSearchParams
  • Revision id: 1031512
  • Created:
  • Creator: cuixiping
  • Is current revision? No
  • Comment fix an error in the example code

Revision Content

{{ApiRef("URL API")}}

The URLSearchParams interface defines utility methods to work with the query string of a URL.

An object implementing URLSearchParams can directly be used in a {{jsxref("Statements/for...of", "for...of")}} structure, instead of {{domxref('URLSearchParams.entries()', 'entries()')}}: for (var p of mySearchParams) is equivalent to for (var p of mySearchParams.entries()).

Constructor

{{domxref("URLSearchParams.URLSearchParams", 'URLSearchParams.URLSearchParams()')}}
Constructor returning a URLSearchParams object.

Properties

This interface doesn't inherit any property.

Methods

This interface doesn't inherit any method.

{{domxref("URLSearchParams.append()")}}
Appends a specified key/value pair as a new search parameter.
{{domxref("URLSearchParams.delete()")}}
Deletes the given search parameter, and its associated value, from the list of all search parameters.
{{domxref("URLSearchParams.entries()")}}
Returns an {{jsxref("Iteration_protocols","iterator")}} allowing to go through all key/value pairs contained in this object.
{{domxref("URLSearchParams.get()")}}
Returns the first value associated to the given search parameter.
{{domxref("URLSearchParams.getAll()")}}
Returns all the values association with a given search parameter.
{{domxref("URLSearchParams.has()")}}
Returns a {{jsxref("Boolean")}} indicating if such a search parameter exists.
{{domxref("URLSearchParams.keys()")}}
Returns an {{jsxref("Iteration_protocols", "iterator")}} allowing to go through all keys of the key/value pairs contained in this object.
{{domxref("URLSearchParams.set()")}}
Sets the value associated to a given search parameter to the given value. If there were several values, delete the others.
{{domxref("URLSearchParams.toString()")}}
Returns a string containg a query string suitable for use in a URL.
{{domxref("URLSearchParams.values()")}}
Returns an {{jsxref("Iteration_protocols", "iterator")}} allowing to go through all values of the key/value pairs contained in this object.

Example

var paramsString = "q=URLUtils.searchParams&topic=api"
var searchParams = new URLSearchParams(paramsString);

//Iterate the search parameters.
for (let p of searchParams) {
  console.log(p);
}

searchParams.has("topic") === true; // true
searchParams.get("topic") === "api"; // true
searchParams.getAll("topic"); // ["api"]
searchParams.get("foo") === null; // true
searchParams.append("topic", "webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev"
searchParams.set("topic", "More webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev"
searchParams.delete("topic");
searchParams.toString(); // "q=URLUtils.searchParams"

Specifications

Specification Status Comment
{{SpecName('URL', '#urlsearchparams', "URLSearchParams")}} {{Spec2('URL')}} Initial definition.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(49)}} {{CompatGeckoDesktop("29.0")}} {{CompatNo}} 36 {{CompatNo}}
entries(), keys(), values(), and support of for...of {{CompatChrome(49)}} {{CompatGeckoDesktop("44.0")}} {{CompatNo}} 36 {{CompatNo}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatChrome(49)}} {{CompatGeckoMobile("29.0")}} {{CompatNo}} {{CompatUnknown}} {{CompatNo}} {{CompatChrome(49)}}
entries(), keys(), values(), and support of for...of {{CompatNo}} {{CompatChrome(49)}} {{CompatGeckoMobile("44.0")}} {{CompatNo}} {{CompatUnknown}} {{CompatNo}} {{CompatChrome(49)}}

See also

Revision Source

<p>{{ApiRef("URL API")}}</p>

<p>The <strong><code>URLSearchParams</code></strong> interface defines utility methods to work with the query string of a URL.</p>

<p>An object implementing <code>URLSearchParams</code> can directly be used in a {{jsxref("Statements/for...of", "for...of")}} structure, instead of {{domxref('URLSearchParams.entries()', 'entries()')}}: <code>for (var p of mySearchParams)</code> is equivalent to <code>for (var p of mySearchParams.entries())</code>.</p>

<h2 id="Constructor">Constructor</h2>

<dl>
 <dt>{{domxref("URLSearchParams.URLSearchParams", 'URLSearchParams.URLSearchParams()')}}</dt>
 <dd>Constructor returning a <code>URLSearchParams</code> object.</dd>
</dl>

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

<p><em>This interface doesn't inherit any property.</em></p>

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

<p><em>This interface doesn't inherit any method.</em></p>

<dl>
 <dt>{{domxref("URLSearchParams.append()")}}</dt>
 <dd>Appends a specified key/value pair as a new search parameter.</dd>
 <dt>{{domxref("URLSearchParams.delete()")}}</dt>
 <dd>Deletes the given search parameter, and its associated value, from the list of all search parameters.</dd>
 <dt>{{domxref("URLSearchParams.entries()")}}</dt>
 <dd>Returns an {{jsxref("Iteration_protocols","iterator")}} allowing to go through all key/value pairs contained in this object.</dd>
 <dt>{{domxref("URLSearchParams.get()")}}</dt>
 <dd>Returns the first value associated to the given search parameter.</dd>
 <dt>{{domxref("URLSearchParams.getAll()")}}</dt>
 <dd>Returns all the values association with a given search parameter.</dd>
 <dt>{{domxref("URLSearchParams.has()")}}</dt>
 <dd>Returns a {{jsxref("Boolean")}} indicating if such a search parameter exists.</dd>
 <dt>{{domxref("URLSearchParams.keys()")}}</dt>
 <dd>Returns an {{jsxref("Iteration_protocols", "iterator")}} allowing to go through all <strong>keys </strong>of the key/value pairs contained in this object.</dd>
 <dt>{{domxref("URLSearchParams.set()")}}</dt>
 <dd>Sets the value associated to a given search parameter to the given value. If there were several values, delete the others.</dd>
 <dt>{{domxref("URLSearchParams.toString()")}}</dt>
 <dd>Returns a string containg a query string suitable for use in a URL.</dd>
 <dt>{{domxref("URLSearchParams.values()")}}</dt>
 <dd>Returns an {{jsxref("Iteration_protocols", "iterator")}} allowing to go through all <strong>values </strong>of the key/value pairs contained in this object.</dd>
</dl>

<h2 id="Example">Example</h2>

<pre class="brush: js">
var paramsString = "q=URLUtils.searchParams&amp;topic=api"
var searchParams = new URLSearchParams(paramsString);

//Iterate the search parameters.
for (let p of searchParams) {
&nbsp; console.log(p);
}

searchParams.has("topic") === true; // true
searchParams.get("topic") === "api"; // true
searchParams.getAll("topic"); // ["api"]
searchParams.get("foo") === null; // true
searchParams.append("topic", "webdev");
searchParams.toString(); // "q=URLUtils.searchParams&amp;topic=api&amp;topic=webdev"
searchParams.set("topic", "More webdev");
searchParams.toString(); // "q=URLUtils.searchParams&amp;topic=More+webdev"
searchParams.delete("topic");
searchParams.toString(); // "q=URLUtils.searchParams"
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('URL', '#urlsearchparams', "URLSearchParams")}}</td>
   <td>{{Spec2('URL')}}</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</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(49)}}</td>
   <td>{{CompatGeckoDesktop("29.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>36</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>entries()</code>, <code>keys()</code>, <code>values()</code>, and support of <code>for...of</code></td>
   <td>{{CompatChrome(49)}}</td>
   <td>{{CompatGeckoDesktop("44.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>36</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49)}}</td>
   <td>{{CompatGeckoMobile("29.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49)}}</td>
  </tr>
  <tr>
   <td><code>entries()</code>, <code>keys()</code>, <code>values()</code>, and support of <code>for...of</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49)}}</td>
   <td>{{CompatGeckoMobile("44.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49)}}</td>
  </tr>
 </tbody>
</table>
</div>

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

<ul>
 <li>Other URL-related interfaces: {{domxref("URL")}}, {{domxref("URLUtils")}}.</li>
 <li><a href="https://developers.google.com/web/updates/2016/01/urlsearchparams?hl=en">Google Developers: Easy URL manipulation with URLSearchParams</a></li>
</ul>

<dl>
</dl>
Revert to this revision