0

If my browser is on http://example/cars?type=ford, searchParams.get(type) returns null, and if it on http://example/cars?type=ford&type=dodge, it returns dodge. How can the first parameter after the question mark be included. I am operating Chrome Version 64.0.3282.167 (Official Build) (64-bit).

my.filter=function(type,value){
    var searchParams = new URLSearchParams(window.location.href);
    var search=searchParams.get(type);
    console.log(search)
    if(search) {
        search=search.split(',');
        if(!search.indexOf(value)) {
            search.push(value);
            searchParams.set(type,search.join(','));
        }
    }
    else searchParams.append(type,value);
    var url=searchParams.toString();
    console.log(url);
    //window.location.href = url;
}
2
  • 2
    Why are you passing in window.location.href, and not window.location.search …? Commented Feb 26, 2018 at 12:25
  • @CBroe No good reason, just ignorance. Thank you. Commented Feb 26, 2018 at 12:27

1 Answer 1

0
!search.indexOf(value)

indexOf returns -1 (a true value) if there is no match, but 0 (a false value) if there is a match at position 0.

You can't test for falseness to see if there isn't a match!

Test against -1 instead.

search.indexOf(value) === -1
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.