1

Possible Duplicate:
How can I get query string values?

Hello I have a page with a url like

mypage.com?iid=11&pid=1

In there im running some ajax and need to pass the idd from my querystring to this code block

$.ajax({
dataType : "html" ,
url: "stream.php?iid=[GET iid]&lastComment="+ $(".postedComment:last").attr('id') , 

But am not experienced enough with JS to get my syntax correct. What would i use in place of [GET iid] to pass that variable?

<?php $_GET['iid'];?> 

won't work since its a JS file.

Thanks

Community
  • 1
  • 1
Hector
  • 682
  • 3
  • 14
  • 27

2 Answers2

0

There is no standard solution. Everyone has his own solution.

Personally I sometimes use this function :

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

like this :

$.ajax({
dataType : "html" ,
url: "stream.php?iid="+getUrlParameter('iid')+"&lastComment="+ $(".postedComment:last").attr('id') , 
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Here is the answer

How can I get query string values in JavaScript?

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Community
  • 1
  • 1
Mik
  • 1,705
  • 1
  • 14
  • 26
  • 1
    Vote to close as a duplicate (or flag for the mods) instead of copy+pasting the code here. But thanks for the research :-) – Bergi Nov 29 '12 at 20:20