0

I've this URL here:

http://localhost.com/?color=Red,Blue,Green

How can I check now with JavaScript how many values are set to the parameter separated by the comma?

So something like:

if (url.parameter.length > 1) {
    alert('More then 1 parameters');
} else {
    alert('Just one');
}
3
  • Do your values need to be comma seperated. Because there are more convenient ways Commented Dec 8, 2018 at 0:58
  • @SebastianSpeitel this is not so important. I can also be a + or /. But my whole implementation is currently based on this format so I need to change a lot of things. Commented Dec 8, 2018 at 1:00
  • "http://localhost.com/?color=Red,Blue,Green".split("?")[1].split(",").length Commented Dec 8, 2018 at 1:20

1 Answer 1

1

You can use URLSearchParams to get the value and then just split it by the ,.

const url = 'http://localhost.com/?color=Red,Blue,Green';

const colors = new URL(url).searchParams.get('color').split(',');
console.log(colors);
if (colors.length > 1) {
  alert('More then 1 parameters');
} else {
  alert('Just one');
}

Sign up to request clarification or add additional context in comments.

1 Comment

This don't work in the IE browser. Is there a solution which will work?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.