Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/webgalli/public_html/blog/wp-includes/plugin.php on line 571

Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/webgalli/public_html/blog/wp-includes/plugin.php on line 571
how to get url parameters with jquery or javascript - Team Webgalli Blog
Request quote

Get URL parameters using jQuery

Posted on: June 27th, 2012 by Raez Mon 3 Comments

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: , , ,

3 Responses

  1. Hank Rouse says:

    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?

  2. Team Webgalli'an says:

    You can add the above function to parse the URL queries, between the tags. See this fiddle

  3. Karthik Sekar says:

    Finally i got in here. Thanks for the nice snippet i was looking for.