Using Javascript to get URL Vars, not working when multiple values present in QueryString

Posted by bateman_ap on Stack Overflow See other posts from Stack Overflow or by bateman_ap
Published on 2010-05-17T13:32:47Z Indexed on 2010/05/17 13:40 UTC
Read the original article Hit count: 125

Filed under:
|
|

Hi, I am using a Javascript function to get the values of a URL to pass to jQuery using the function below:

 function getUrlVars()
    {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
    return vars;
    }

And then setting the value like this:

 var type = getUrlVars()["type"]

This all works perfectly, however I have just come into a situation where I need to get multiple values, one of my form elements are checkboxes where multiple values can be checked, so my URL will look something like this:

 http://www.domain.com/test.php?type=1&cuisine[]=23&cuisine[]=43&name=test

If I alert out the cuisine value using the function above I only ever get the final value:

 alert (getUrlVars()["cuisine[]"]);

Would alert "43".

What I would like it to be is a comma delimited string of all "cuisine" values. ie in the above example "23,43"

Any help very welcome! In case any solution requires it I am using PHP 5.3 and Jquery 1.4

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about querystring