This simple code snippet will help you in troublesome times to get the query parameters from a URL with jquery / javascript.
/**
* Function to parse the URL queries
*/
function get_url_parmeters(url, name) {
name = name.replace(/[[]/, "\[").replace(/[]]/, "\]");
var regexS = "[\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
if (results == null) return "";
else return results[1];
}
To use it just do
var url = "http://webgalli.com/?query1=123&query2=456"; var param1 = get_url_parmeters(url, 'query1'); alert(param1);
By using above function, we can easily get particular query string from client side script. If we send a value to above function and suppose there is no query string with that name, then it will return empty string. Hence we can easily accessing query string from client side using JavaScript.
Tags: get url parameters with javascript, javascript url query, jquery url, jquery url parameters

I have two variables: “coachid” & “screenname”
So want to use:
?coachid=186123&screenname=Hank247Fitness
I don’t know how to implement the above into my script. What do I place in the head of the html doc?
You can add the above function to parse the URL queries, between the
tags. See this fiddleFinally i got in here. Thanks for the nice snippet i was looking for.