开发者

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 tha开发者_C百科t? Does JS store them in a dict somewhere? Or is there a jQuery solution?

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


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)


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');


turns out this has been asked before

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜