0

My URL looks like this one: http://www.something.hu/#/ask?kerdesPost=fdasdas%20ad%20asd%20ad%20asdas

I would like to get only fdasdas%20ad%20asd%20ad%20asdas or only kerdesPost=fdasdas%20ad%20asd%20ad%20asdas.

How can I do it via JavaScript? What is the shortest way?

user2301881
  • 320
  • 5
  • 16
  • I tried that like this: `function getParameterByName(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regex = new RegExp("[\\?&]" + name + "=([^]*)"), results = regex.exec(location.search); return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); } var alma = getParameterByName('kerdesPost'); alert(alma); `, But gives me empty alert window. – user2301881 May 17 '13 at 12:19

1 Answers1

7

You can try the following:

if((window.location.href).indexOf('?') != -1) {
    var queryString = (window.location.href).substr((window.location.href).indexOf('?') + 1); 

    // "queryString" will now contain kerdesPost=fdasdas%20ad%20asd%20ad%20asdas

    var value = (queryString.split('='))[1];

    // "value" will now contain fdasdas%20ad%20asd%20ad%20asdas

    value = decodeURIComponent(value);

    // "value" will now contain fdasdas ad asd ad asdas (unescaped value)
}

JSFiddle

This should get you what you need.

War10ck
  • 12,387
  • 7
  • 41
  • 54