Reputation: 282825
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
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
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