0

I've been strugling looking for a way to set a cookie if a URL parameter is persent. The problem is, the name of the URL parameter is half dynamic.

The URL would be:

http://zzzz.test.bbbb/index.html?transaction[XXXX][zzzzz]=YYYYY

Where XXXX and zzzzz are part o the URL name but can change according to what's in the link.

How would the correct getParameterByName function look like in order to recognize the URL parameter transaction[XXXX][zzzzz] ?

I've tried this but it does not work:

function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp(name + '(?:\\[\\d+\\]?:\\[\\d+\\])?=' + '(.+?)(&|$)'),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

if (getParameterByName("transaction")) {
set_cookie ("Transaction_ID", getParameterByName("transaction"));
}

Any ideas?

adrian.m123
  • 85
  • 1
  • 3
  • 11
  • what are the type of values that xxxx and zzzz can be? Is it only alphabets? or alphabets and numbers? Also is it enough if cookie be set if only the word transaction is present? – wallop Sep 27 '14 at 09:38
  • Also is transaction always the first query parameter? – wallop Sep 27 '14 at 09:40
  • @Wishy xxxx and zzzz can be letters or numbers. The parameter can be anywhere in the link. If it's the first one it will appear as `?transaction[XXXX][zzzzz]=YYYYY` if it's not the first it appears a `&transaction[XXXX][zzzzz]=YYYYY` – adrian.m123 Sep 27 '14 at 10:00
  • The word `transaction`is only the name of the URL parameter, I will be setting the cookie by running the function `if (getParameterByName("transaction")) { set_cookie ("Transaction_ID", getParameterByName("transaction")); }` – adrian.m123 Sep 27 '14 at 10:14
  • adrian i am sorry i didn explain more clearly. I wanted to create a regular expression for you so i asked those questions. I very well know what the meaning of those are. – wallop Sep 27 '14 at 11:34
  • Oh I see. Yes, `transaction` is always the first query parameter. And yes, it is enough if the cookie is set when the word `transaction` is present in the URL – adrian.m123 Sep 27 '14 at 11:42
  • well i have attached a code below which will work even if transaction is not the first query parameter and it will check for entire transaction[xxxx][zzzzz] instead of just 'transaction' – wallop Sep 27 '14 at 12:16

1 Answers1

0

Check this JSBIN. This will help you. Replace locatStr by window.location.search; The below code will work in all scenarios

function getParameterByName(){
   var locatStr = '?xyz=123&transaction[XXXX][zzzzz]=YYYYY',
       searchStr = locatStr.split('?')[1],
       matchArr = searchStr.match(/transaction\[[a-zA-Z0-9]+\]\[[a-zA-Z0-9]+\]/gi),       
       para;

  if(matchArr){
          var temp = searchStr.split(matchArr[0]+'=')[1];
        return ((temp.indexOf('&')!=-1) ? temp.split('&')[0] : temp);
      }
      else{
        return false;
      }

}


var param = getParameterByName();
console.log(param);
if(param){
  console.log('set cookie here');
}
else{
  console.log('no cookie present');
}

P.S. Dont forget to accept the answer if satisfied

wallop
  • 2,510
  • 1
  • 22
  • 39
  • The cookie is set but the value stays as `YYYYY`. The cookie value should be also dynamic according to what is in the URL. – adrian.m123 Sep 27 '14 at 16:17
  • i mentioned above please change locatStr = window.location.search. Value is whatever that appears in the url. – wallop Sep 28 '14 at 18:26