4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

javascript : get array from url querystring

javascript : get array from url querystring


/*

 * Returns a map of querystring parameters

 * 

 * Keys of type <fieldName>[] will automatically be added to an array

 *

 * @param String url

 * @return Object parameters

 */

function getParams(url) {

    var regex = /([^=&?]+)=([^&#]*)/g, params = {}, parts, key, value;


    while((parts = regex.exec(url)) != null) {


        key = parts[1], value = parts[2];

        var isArray = /\[\]$/.test(key);


        if(isArray) {

            params[key] = params[key] || [];

            params[key].push(value);

        }

        else {

            params[key] = value;

        }

    }


    return params;

}


url = 'http://www.somewhere.com/index.html?field[]=history&field[]=science&field[]=math';

getParams(url);


0 upvotes $0.00

Replies (0)

Conversation

No replies yet

Replies will appear here as people join the conversation.

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: