Link to home
Start Free TrialLog in
Avatar of Pete Winter
Pete WinterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Jquery replace URL parameters

I am replacing my URL parameters using the below code...

var myParams = jQuery.param(myParams);
history.replaceState(null, null, "?" + myParams);

Open in new window


then I am trying to get the results using this code...

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}​

var tech = GetURLParameter('technology');

Open in new window


Example showed on this page: http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html

The above is fine for fields that have just one value, but some of my field have more than one value. For example...

http://dummy.com/?technology=jquery&technology=php&blog=jquerybyexample

var tech = GetURLParameter('technology');

Only gets the first value. How would get all values that are associated to 'technology'?
Avatar of Bill Prew
Bill Prew

Currently the return from GetURLParameter() is a string with the value associated with that parm.  If there are multiple values how would you want those returned?


»bp
Avatar of Pete Winter

ASKER

You will see from my example the 'technology' param is repeated with a second value. However currently it only outputs one value. How can  I output all the technology values as an array?
So you would always want the return from that function to be an array, which would be the case even if there was just one value for that param, that would be okay?  And then your calling code would have to be adjusted to expect and deal with an array.


»bp
An array is fine for all if easier. Can you supply an example of what to do please?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks