Javascript get the query string param from url

So, you want to get the query string parameters after the question mark from an URL such as 

The code snippet below will do. It will return you back an json object {id:’123456′,name:’amy’}

function getUrlQueryParams()
    var queryParams = {}, param;
    var params ="&");
    for(var i = 0; i < params.length; i++)
        param = params[i].split('=');
    return params;

var queryParams = getUrlQueryParams();

The stores the query string of the URL including the question mark. With the above example URL, the value of will be “?id=123456&name=amy”. Then we use the substring function to get rid of the question mark and then split the string into an array of param pairs. At this point, each element will be a string of varaible and value pairs connected with the equal sign(=). Next, loop through the array, split each variable and value pair by the equal sign and add them to the json object.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search