0

This function works only for a parameter.

function getQueryStringValue(key) {
   debugger;
   return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
};

Please I need a JavaScript function that can retrieve more than one querystring parameter, I need to pass the name of the parameter as key to the function. Thank you

The function in the link Alex shared is as below


    function getParameterByName(name, url) {
        if (!url) {
            url = window.location.href;
        }
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    };

With my url as this:




       var url= 'search-results.html?param1=unth?param2=lagos';

And I pass this to the function :


     var param1 = getParameterByName('param1');
     var param2 = getParameterByName('param2');

It return param1 as : luth?param2=lagos instead of luth.

This is the same issue with the function I shared. My question is a JavaScript Function that retrieves multiple querystring parameter but the function works only for one parameter

O. Okoye
  • 52
  • 1
  • 10
  • 1
    Possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Alex K. Feb 23 '17 at 14:55
  • Thank you Alex. I will test the function in the link and get back to you. – O. Okoye Feb 23 '17 at 15:04

1 Answers1

0

Your URL should be: var url= 'search-results.html?param1=unth&param2=lagos';

In this case function will work.

var param1 = getParameterByName('param1'); //return unth

var param2 = getParameterByName('param2'); //return lagos

SouXin
  • 1,565
  • 11
  • 17