Hi
I have recently taken up Javascript after a couple of years away from coding, so am finding this pretty tough to get my head around.
What I am trying to do is to set up the Google Analytics tracking code in an external Javascript file.
I do not want the analytics code to be outputted/set on the page unless the user chooses to accept it (This is relating to recent changes to the cookie legislation.). If the user does not choose to accept Analytics, the script runs without setting the analytics code.
I decided to use an event handler to deal with this, and I have stored the anlaytics code itself in a function, which is then passed to another function - disp_confirm() - in order for it to have an id applied to it. The plan then is to call this id (and therefore call the event handler) from the just above the closing body tag in the html page (I decided against putting the analytics code in the header because I was under the impression that the event handlers wouldn't process successfully if the code was implemented here).
The problem is, I don't know whether this will work, or if I am trying to do something that cannot be done with Javascript.
The idea of passing a function to another function doesn't sit well with me - so I'm sure I must be way off the mark with this....I'm not sure if I'm using the correct syntax to pass one function to another either
Anyway, here's my code. I'd really appreciate some advice on this, especially if I am way off mark with my approach!!
Thanks
window.onload = disp_confirm;
function disp_confirm()
{
var r=confirm("This website uses Google Analytics to monitor website use which helps us offer a better service to you. However, No personal data is stored. If you wish to proceed with cookies, click ok. Alternatively click cancel.")
if (r==true)
{
var gas = passString.analytics();
document.getElementById('gas');
}
else
{
alert("Google cookies have been disabled")
}
}
function analytics()
{
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
});
}
Any help advice would be really appreciated....
Thanks