Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why extend jQuery? What would be the benefit of extending jQuery vs just having a global function?</p> <pre><code>function qs(key) { key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&amp;"); // escape RegEx meta chars var match = location.search.match(new RegExp("[?&amp;]"+key+"=([^&amp;]+)(&amp;|$)")); return match &amp;&amp; decodeURIComponent(match[1].replace(/\+/g, " ")); } </code></pre> <p><a href="http://jsfiddle.net/gilly3/sgxcL/">http://jsfiddle.net/gilly3/sgxcL/</a></p> <p>An alternative approach would be to parse the entire query string and store the values in an object for later use. This approach doesn't require a regular expression and extends the <code>window.location</code> object (but, could just as easily use a global variable):</p> <pre><code>location.queryString = {}; location.search.substr(1).split("&amp;").forEach(function (pair) { if (pair === "") return; var parts = pair.split("="); location.queryString[parts[0]] = parts[1] &amp;&amp; decodeURIComponent(parts[1].replace(/\+/g, " ")); }); </code></pre> <p><a href="http://jsfiddle.net/gilly3/YnCeu/">http://jsfiddle.net/gilly3/YnCeu/</a></p> <p>This version also makes use of <code>Array.forEach()</code>, which is unavailable natively in IE7 and IE8. It can be added by using <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach#Compatibility">the implementation at MDN</a>, or you can use jQuery's <a href="http://api.jquery.com/jquery.each/"><code>$.each()</code></a> instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload