Getting query string parameters from clean/SEO friendly URLs with JavaScript
<p>I've recently switched my site to use clean/SEO-friendly URLs which has now caused me some issues with a JavaScript function I had for grabbing the query string parameters.</p>

<p>Previously I had been using this function:</p>

<pre><code>function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&amp;");
for (var i = 0; i &lt; vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
        return pair[1];
    }
}
return (false);
}
</code></pre>

<p>Which worked fine on things like <a href="http://www.example.com/index.php?id=1&amp;image=awesome.jpg" rel="nofollow">this</a> as you could just call <code>getQueryVariable("image")</code> and return "awesome.jpg".</p>

<p>I've been playing with the <code>indexOf();</code> function to try and grab the relevant parameters from the current URL, eg:</p>

<pre><code>var url = window.location.pathname;
var isPage = url.indexOf("page") + 1;
</code></pre>

<p>In an attempt to get the array index number of the "page" parameter, and then plus 1 it to move along to the value of that <code>(?page=name &gt; /page/name/)</code></p>

<p>JavaScript isn't my main language, so I'm not used to working with arrays etc and my attempt to turn this into a new function has been giving me headaches.</p>

<p>Any pointers?</p>