Click here to Skip to main content
15,884,862 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
i am using two anchor tags with href property contains query strings.. 1st contains href like default.aspx?cat=paper and 2nd contains href like default.aspx?subcat=pen

i have binded onclick event with anchor tags..

suppose when user clicks on 1st anchor tag url will be like default.aspx?cat=paper

my problem starts from here..

when user clicks on 2nd url then javascript code should check for url that is subcat query string already exits if yes then update the existing subcat with new id and if not then simply add url as it is.. Actually i want never two query string with the same name should exists in the url (no duplicacy of query strings).
Posted
Comments
Dholakiya Ankit 5-Oct-13 3:34am    
But why you have a same subcat? give me example

1 solution

You can do above task as below.

Step 1 : Get the query string from the 1st Url

C#
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}


As an example:

href="http://www.example.com/default.aspx?cat=paper

Above code will return :

{
    "cat"    : "paper",
}


Then you can get that value is as below:

var cat = getUrlVars()["cat"];


Step 2: Append above value when user clicks the 2nd url

For more info:

jquery get querystring from URL


I hope this will help to you.
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900