0

Can someone please tell me how I can add in the address bar variable to my javascript?

The website address reads: www.example.com/index.php?user=74

and the javascript that needs to include 'user = 74' is:

xmlhttp.open("GET","../profile/" + pageName + ".php",true);
user2527750
  • 51
  • 1
  • 11
  • possible duplicate of [Get URL parameter with jQuery](http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery) – bfavaretto Aug 23 '13 at 21:33
  • 1
    possible duplicate of [How can I get query string values?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values) – Tim B James Aug 23 '13 at 21:34
  • @TimBJames, the problem with those old questions is that it's hard to find an actual answer. All the top voted answers are full of bugs, and can't handle edge cases. Good luck parsing a query string such as `?&==;`. – zzzzBov Aug 23 '13 at 21:39
  • @zzzzBov Yeah that is why it is "possible duplicate" e.g. A hint towards maybe finding the answer. – Tim B James Aug 23 '13 at 21:39
  • @zzzzBov Then add a better answer to one of those questions. Or do you think "the answer" will be posted here if we just leave this question open? – bfavaretto Aug 23 '13 at 21:47
  • @bfavaretto, I'm sorry if I implied that this question should stay open. I was simply bringing up the age bias for bad answers. – zzzzBov Aug 23 '13 at 22:04
  • @zzzzBov Sorry if I sounded harsh. My point is, if those answers have problems, it would be great if someone knowledgeable enough could fix them, or add a better answer. Not that I'm suggesting that *you* should do that *on a Friday night* :) – bfavaretto Aug 23 '13 at 22:19

1 Answers1

0
function get_query(){
  var url = location.href;
  var qs = url.substring(url.indexOf('?') + 1).split('&');
  for(var i = 0, result = {}; i < qs.length; i++){
    qs[i] = qs[i].split('=');
    result[qs[i][0]] = decodeURIComponent(qs[i][1]);
  }
  return result;
}

xmlhttp.open("GET","../profile/" + pageName + ".php?user=" + get_query()['user'],true);
Paul
  • 139,544
  • 27
  • 275
  • 264
  • But that would hard code it to the user always being 74 no matter what the address bar says. I need it to change when a different number appears in the address bar. It need to retrieve the address bar value somehow – user2527750 Aug 23 '13 at 21:35
  • @user2527750 Misunderstood your question. I have updated it using my Answer to the question that `Tim B James` linked to. It should work now. – Paul Aug 23 '13 at 21:36