Ishita
Ishita

Reputation: 87

Modify a parameter in url with page reload using js/jquery

I have a url like result?graphType=default&product=xyzzzz&shop=11

I just want to change value of graphType, when user clicks a button. On click of that button i store some value in a variable, and i want to replace graphType=default with graphType=my_new_value in my url without reload. I don't understand how to split url at graphType= . and even if I am able to split to url at graphType, how can I replace default (which can be something else as well depending on user options) with my variable

Upvotes: 3

Views: 3957

Answers (1)

Haresh Vidja
Haresh Vidja

Reputation: 8496

I think your solution should be like this

$(document).ready(function(){
    var queries = {};
    $.each(document.location.search.substr(1).split('&'), function(c,q){
        var i = q.split('=');
        queries[i[0].toString()] = unescape(i[1].toString()); // change escaped characters in actual format
    });

    // modify your parameter value and reload page using below two lines
    queries['your key']='your value';

    document.location.href="?"+$.param(queries); // it reload page
    //OR
    history.pushState({}, '', "?"+$.param(queries)); // it change url but not reload page
});

Upvotes: 7

Related Questions