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

2

1 Answer 1

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

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

1 Comment

Thank you a lot SouXin. You are right. It is working now after the amendment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.