replace anchor tag href with variable from url string javascript
<p>I have following java script code:</p>

<pre><code>var product = getQueryVariable("product");

$('a.link').each(function()
{
    $(this).attr("href", $(this).attr("href") + "&amp;product=" + getQueryVariable("product"));
});

function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);        

    var variables = query.split("&amp;");

    for (var iCount = 0; iCount &lt; variables.length; iCount++)
    {
        var pair = variables[iCount].split("=");

        if (pair[0] == variable)
            return pair[1];
    }
}
</code></pre>

<p>If url is: <code>http://www.xxxx.com/index.htm?product=abc</code>, It sets a link to an anchor tag</p>

<pre><code>&lt;a class="link" href="http://www.aaaa.com/index.htm"&gt;Click here&lt;/a&gt;
</code></pre>

<p>as</p>

<pre><code>&lt;a class="link" href="http://www.aaaa.com/index.htm?product=abc"&gt;Click here&lt;/a&gt;
</code></pre>

<p>which is what I want, and it does it well.</p>

<p>But if anchor tag already has <code>product</code> variable in its <code>href</code> like</p>

<pre><code>&lt;a class="link" href="http://www.aaaa.com/index.htm?product=xyz"&gt;Click here&lt;/a&gt;
</code></pre>

<p>then I get</p>

<pre><code>&lt;a class="link" href="http://www.aaaa.com/index.htm?product=xyz&amp;product=abc"&gt;Click here&lt;/a&gt;
</code></pre>

<p>while I was expecting that if <code>product</code> variable is present in <code>href</code> of anchor tag, it will replace existing product variable. And if there is no product variable in <code>href</code> tag, it will append it.</p>

<p>The code appends well, but does not replace existing <code>product</code> variable in <code>href</code> of anchor tag.</p>

<p>So, the question is how do I get <code>'product'</code> param from all <code>a</code> tags that have class <code>'link'</code>? so that I can replace it in my code if found. And can I fetch it in <code>$('a.link').each(function()</code> ?</p>

<p>How can I do it?</p>