0

I have jQuery that gets parameters from form select options and adds it to the URL for search. But at the moment my JS removes the old stuff and replaces with new always. Here's the current one:

$("#year").change(function() {
        var path = $(location).attr('href').split('?', 1);;
        var year = $(this).val();
        var search = path + '?year=' + year;

        if ($(this).val() != '') {
            window.open(search, '_self');
        }
    });

    $("#type").change(function() {
        var path = $(location).attr('href').split('?', 1);;
        var type = $(this).val();
        var search = path + '?type=' + type;

        if ($(this).val()) {
            window.open(search, '_self');
        }
    });

So if user selects from another option the would be: www.domain.com/search?type=something or www.domain.com/search?year=2011

But what I would like to do is that the user could choose both parameters. So the URL could be www.domain.com/search?type=something&year=2011 but I'm not sure how to do it.

I tried something but then it always added new parameters to the end and the URL ended up being like this: www.domain.com/search?type=something&year=2011&type=something&year=2011&type=something&year=2011

Thanks for help!

2 Answers 2

1

You can use regexp objects and parse query string for & and ? to understand is there some params.

So, here's js-code for main idea ( http://jsfiddle.net/RtCte/ ):

function replaceOrAdd(str, pattern, newValue) {
   if (str.match(pattern)) {
    // parameter is already defined
    document.writeln(str.replace(pattern, newValue) + '<br />');
   }
   else if (str.indexOf("?") != -1) {
    // query string exists, but without this parameter
    document.writeln(str + "&" + newValue + '<br />');
   }
   else {
    // there is no query string
    document.writeln(str + "?" + newValue + '<br />');
   }
}

var str = ["www.domain.com/search?type=something&year=2011",
"www.domain.com/search?year=2011&type=something",
"www.domain.com/search?type=something",
"www.domain.com/search?year=2011",
"www.domain.com/search"];

var patt1 = /type=[^&^$]+/i;
var patt2 = /year=[^&^$]+/i;

var newValue1 = "type=new"
var newValue2 = "year=1999"

for (var i = 0; i < str.length; i++) {
   replaceOrAdd(str[i], patt1, newValue1);
}
document.write("<br/>");
for (var i = 0; i < str.length; i++) {
   replaceOrAdd(str[i], patt2, newValue2);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could parse all the current querystrings to an object/array, then update or add to that object and lastly build the new querystring from that object. That would make sure there's always one of each and that they're correctly set.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.