mpen
mpen

Reputation: 282825

Pass along get params?

I want to use ajax to retrieve some JSON from another page, but I want to pass along the same GET params that were used to request the original page. How do I do that? Does JS store them in a dict somewhere? Or is there a jQuery solution?

$.ajax({
    url: 'mysecretwebpage.com/supersecret',
    data: ???
});

Upvotes: 0

Views: 143

Answers (3)

mpen
mpen

Reputation: 282825

turns out this has been asked before

Upvotes: 1

Slappy
Slappy

Reputation: 4092

I got this handy function:

document.getParameterByName = function (name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
};

Use it like:

var paramValue = document.getParameterByName('paramName');

Upvotes: 2

Dr.Molle
Dr.Molle

Reputation: 117314

The data you need you'll find in

window.location.search

Remove the first char from this string(will be the question mark, if GET is not empty)

Upvotes: 3

Related Questions