views:

31

answers:

3

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: ???
});
+3  A: 

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)

Dr.Molle
I also want to add a value... I can append it to the string, but what if it already exists? I'd need to replace it.
Mark
Dr.Molle
+2  A: 

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');
Slappy
Don't really want a single param name... I want a dict. And I'm pretty sure you yoinked that off the net somewhere.. at least link back to the article you stole it from.
Mark
Actually I "yoinked" it from a project I am working on.
Slappy
A: 

turns out this has been asked before

Mark