promotion

Sunday, March 22, 2015

get url parameters and values using jquery

Here is the simple method to get url parameters & values using jquery.
function getUrlParams() {
    var url = document.location.href;
    var params = {},hash;
    var params_arr = url.slice(url.indexOf('?') + 1).split('&');
    for(var i=0; i  < params_arr.length; i++){
      hash = params_arr[i].split("=");
      params[hash[0]] = hash[1];
    }
    return params;
  }
Above method gets the current page URL and split into parameters and their values object. For example:
http://faqspoint.blogspot.in?src=google&module=bottom
Result:
{ src : "google", module : "bottom" }
To get the values of parameters:
var source = getUrlParams()["src"];    //It will return "google"
var module = getUrlParams()["module"];    //It will return "bottom"

No comments:

Post a Comment