tags:

views:

51

answers:

1

Hi all, I would like to insert a parameter value found the url into my javascript code.. So basically i have the following url www.rene-zamm.com/mp-dthanks.asp?gglid=123123123

Now i have the following javascript code in the same page and i want that number in the url to be visible also in the javascript code below:

var gwoTracker=_gat._getTracker("UA-1639156-3");
gwoTracker._trackPageview("/**NUMBER OF URL HERE**/goal");

Can someone help me please?

A: 

You need to parse the querystring. Javascript has no inbuilt way of doing this easily, but there are some nice functions you can use:

function gup( 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 results[1];
}

http://www.netlobo.com/url_query_string_javascript.html

Then in your example you could do:

var queryData = gup("gglid");
var gwoTracker=_gat._getTracker("UA-1639156-3"); gwoTracker._trackPageview("/" + queryData  + "/goal");
Tom Gullen
I have changed my code but still the number is not being visible in the code.the returngglid is appearing as text not as number:<script type="text/javascript">function gup(name){ name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\? var regex = new RegExp(regexS); var results = regex.exec(window.location.href); if( results == null ) return ""; else return results[1];}try {var returngglid = gup("gglid");var gwoTracker=_gat._getTracker("UA-1639156-3");gwoTracker._trackPageview("/" + returngglid + "/goal");}catch(err){}</script>
Rene Zammit
I have tested the code and 'queryData' is returning the correct value. What browser are you using? Any script errors?
Tom Gullen
I have tried it on firefox and IE.... I want that when i click on view source the result will be like this:gwoTracker._trackPageview(/123123123/goal);
Rene Zammit
Ah if you want it to physically print the number in the source you are going to need some server side scripting, such as PHP, ASP.net etc. This is not possible in Javascript unfortunatly.
Tom Gullen
hmmm ok thanks...though when the gwoTracker._trackPageview is called, does it work perfectly? the number will show or the text??
Rene Zammit
It will call it with the number in it yes. Try doing a redirect to that URL as a test and see if it works.
Tom Gullen
thanks for your help. WORKED :) I owe you one hehe
Rene Zammit
No worries, don't forget to upvote answers you like :-)
Tom Gullen