jquery, querystrings, and displaying chunks of javascript code
Hi,
I have to come up with a hack for a client where displaying chunks of Google tracking code depends on the presence of a specific value in a querystring. I found a simple jQuery function to grab the querystring value. The problem/question I have is how to display a chunk of Google tracking code based on the presence of a specific value.
For example, in PHP, you can use %> to switch to output of static HTML and then use <% to switch back to PHP and that, in turns avoids having to escape code, especially since the Google code is javascript itself. Javascript doesn't appear to have that ability to turn on/off. Or does it? I can't find anything online on how to achieve the same effect with Javascript.
Here's the jQuery function:
- $.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});
from this site: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
And here's my jQuery code so far:
- <script type="text/javascript">
var Category = $.getUrlVar('Cat');
if (Category==77) {
alert(Category);
}
</script>
Any ideas how to use Javascript/jQuery would be much appreciated. The template is ASCII html and I don't control the server and the service doesn't provide a way to do this. So I'm left with hacking the html template.
Thanks in advance for any ideas!