0

I'm trying to get all the parameters in a URL even though they don't have a value using JavaScript.

http://example.com/?Germany&France&Norway&Sweden

This URL contains 4 parameters with the name of some countries. I want to get this list of countries in JavaScript, ideally as an array. There are many examples that retrieve the values from key-value parameters, where you have to provide the key and some Regex will return the value.

Stephan
  • 1,791
  • 1
  • 27
  • 51

2 Answers2

0

Try to use split() :

var my_string = "http://example.com/?Germany&France&Norway&Sweden";
var parameters = my_string.split('?')[1];
var params_array = parameters.split('&');
//["Germany","France","Norway","Sweden"]

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
0

Try this:

function noValParams(url) {
  var noValue = [];
  var qp = x.split("?")[1];
  [].forEach.call(qp.split("&"), function (inst) {
    if (inst.indexOf("=") == -1 || inst.indexOf("=") == inst.length - 1) {
      noValue.push(inst);
    }
  });
  return noValue;
}

Live fiddle

Split the URL with a ? and take the index 1. Then split the remaining part with & and check the position of =. Either it should not exist or it should be last index.

This function will return a array of params with value blank.

void
  • 36,090
  • 8
  • 62
  • 107