Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allow cookies to only be created once user gives authorisation

Status
Not open for further replies.

star171

Programmer
Apr 13, 2012
18
GB
Hello,

I am wanting to have a prompt on my webpage load that asks the user if they allow the use of cookies, this is in line with new european directives that are due to come into force.

Once I get the user permission that is when I want the cookie to be set. I have the code to create the cookie and set the expiry on it to be 2 years:
Code:
<script type="text/javascript">
Set_Cookie( 'websitecookie', 'cookiecreated', 730, '/', '', '' )
{

var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires 730*24*60*60000;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

</script>

My code for google analytics is in the page footer, can anyone help with an if statement around the analytics code, which looks to see if the cookie 'websitecookie' has been created then allow the analytics cookies, otherwise do not allow the google analytics cookies to be created.

thank you.
 
You defined the variable [name] sure, but not the variable [websitecookie].

If you intended to have websitecookie be the actual name as a string then you missed the quotes.

Also if you are already passing it to your function then why are you then altering its value.

Code:
function myFunction(var1,var2,varN){
alert(var1);
}
The variables defined in the function declaration, i.e var1, var2, varN are the variables you expect to pass to the function so they are available inside it using those names.

If I do:

Code:
... onclick="myfunction('Hello','How','Are You');" ...
Then var1 will have the value "Hello".
If I then do this:

Code:
var1=someothervar;

I change the value it has to whatever someothervar has.
however if I don't define someothervar then I get an error.

Again if you want the name to be the actual string "websitecookie" you need to add the quotes. Though it defeats the purpose of passing the values to it to begin with.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Vacunita,

I've sorted the cookie set function.

Once the cookie has been created, how can i get it to automatically call my function to check if the cookie has been set and if it has, in the process allow the google analytics cookies to be set?

The code i have for this is:
Code:
function cookieExists(websitecookie){

    if(!websitecookie || websitecookie='websitecookie'){
        return false;
    }
    var cookies = document.cookie.split(";");
    var cookieNames = new Array();
    for( x in cookies){
        cookieNames.push(cookies[x].substr(0,cookies[x].indexOf("=")));
    }
    for(x in cookieNames){
        if(cookieNames[x] == websitecookie){
            return true;
        }
    }
return false;
}
if(cookieExists('websitecookie')){

   //my analytics code here

	var gaJsHost = (("https:" == document.location.protocol) ? "[URL unfurl="true"]https://ssl."[/URL] : "[URL unfurl="true"]http://www.");[/URL]
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


}
</script>
 
Thanks guys,

What the ICO have done is similar to what I am trying as well. I can get the cookie created, that fine.

However what I want help on if possible is in my function cookieExists, in this function I want to check that the cookie named "websitecookie" exists and if it does, then I want the google analytics cookies to be created. From the code I have posted above, can you help with where I may be going wrong?

Alternatively can clicking one button, trigger the Set_Cookie and cookieExists functions?

thanks
 
Alternatively can clicking one button, trigger the Set_Cookie and cookieExists functions?

Yep. Create a function that calls the two functions in the order you want them to be processed.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
I think it can also be set in the onClick in the button to determine the order of the functions to execute.

Is the code i have put ok for checking if the cookie exists and if it does then allow the analytics cookies?
 
Just struggling with the cookieExists function I have which checks if a cookie exists then allows the analytics cookies to be set, would someone please have a look and let me know what i am going wrong?...my cookie nama is websitecookie

Code:
<script type="text/javascript">
function cookieExists(cookieName){

    if(!cookieName || cookieName=''){
        return false;
    }
    var cookies = document.cookie.split(";");
    var cookieNames = new Array();
    for( x in cookies){
        cookieNames.push(cookies[x].substr(0,cookies[x].indexOf("=")));
    }
    for(x in cookieNames){
        if(cookieNames[x] == cookieName){
            return true;
        }
    }
return false;
}

if(cookieExists('websitecookie')){

   //analytics code here

var gaJsHost = (("https:" == document.location.protocol) ? "[URL unfurl="true"]https://ssl."[/URL] : "[URL unfurl="true"]http://www.");[/URL]
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

		var pageTracker = _gat._getTracker("**-******-1");
		pageTracker._setClientInfo(false);
		pageTracker._setAllowHash(false);
		pageTracker._setDetectFlash(false);
		pageTracker._setDetectTitle(false);
		pageTracker._trackPageview();
 


}
</script>

Thanks
 
Hi

Have you checked your browser's console ? All of them are listing the error :
Code:
04/30/2012 04:56:43 PM
[b]invalid assignment left-hand side[/b]
[u]file:///home/master/star171.htm[/u]
[right]Line: 4[/right]
    if(!cookieName || cookieName=''){ 
[green]--------------------------------^[/green]
Code:
17:01:38.474 [highlight black] [/highlight]   [u]file:///home/master/star171.htm[/u]
17:01:38.641 [highlight orange] [/highlight] [b][COLOR=orange]x[/color][/b] invalid assignment left-hand side [right][u]star171.htm:4[/u][/right]
Code:
[COLOR=white red] x [/color] [red]invalid assignment left-hand side[/red]
[green]if(!cookieName || cookieName=''){[/green][right][b][blue]star171.htm (line 4, col 32)[/blue][/b][/right]
[green]----------------------------^[/green]
Code:
Uncaught exception: ReferenceError: Cannot assign to '!cookieName || cookieName'

Error thrown at line 4, column 4 in cookieExists(cookieName) in file://localhost/home/master/star171.htm:
    if(!cookieName || cookieName='')
called from line 20, column 0 in file://localhost/home/master/star171.htm:
    if(cookieExists('websitecookie'))
Code:
[COLOR=white red] x [/color] [red]Uncaught ReferenceError: Invalid left-hand side in assignment[/red] [right][u]star171.htm:4[/u][/right]
In other words :
[ul]
[li][tt]=[/tt] means assignment[/li]
[li][tt]==[/tt] means equality check[/li]
[/ul]


Feherke.
 
Thanks Feherke

I use firebug in firefox sometimes, does this have the error in it?... As im not familiar at all with javascript, im not reallys sure where i need to be looking either to rectify the error..
 
Hi

star171 said:
I use firebug in firefox sometimes, does this have the error in it?
Of course. See the 3[sup]rd[/sup] output I quoted above.
star171 said:
As im not familiar at all with javascript, im not reallys sure where i need to be looking either to rectify the error..
All the error messages are talking about assignment inside an if condition. So ask yourself if you really want to set cookieName to empty string there. If not, see my explanation above regarding the operators.


Feherke.
 
Thanks,

I've got it showing on firebug...

I've amended that if statement to instead of assignment with the one =, i've changed it to == which is equality check. This has sorted the error in firebug, however it still only sets the one cookie, not my analytics cookies...any suggestions, thanks for your help feherke
 
Hi

There are no other problems in the code you posted on 30 Apr 12 9:41. It works for me in my test page. Well, regarding your part of code. It crashes later, after the analytic code sourcing, because no _gat gets defined. I know nothing about Google Analytics, so you will need to check their documentation.

Maybe seeing the entire page would help. If public access is possible, tell us the URL so wee can get the big picture.


Feherke.
 
The page is not on a live site yet, it is only on the development box at work, however i will put the code on the page here for you. currently the page only has a button on it which when clicked creates the cookie. this same button has 2 functions to it's onClick method, first it creates the cookie (Set_Cookie) and then it go to the function cookieExists. Once it goes to the function cookie exists, this is where once it see's the cookie that has been set in the funtion Set_Cookie then it runs the google analytics javascript code, otherwise if no cookie present then it doesn't run the google analytics javascript code to create the analytics cookies. Thanks feherke for your supports.

HEAD of the page

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html lang="en">
<head>
<title>Cookie Test</title>
<script type="text/javascript">

function Set_Cookie(name, value, expires, path, domain, secure )
{
 name = "websitecookie";
 value = "cookiecreated";
// set time

 expires = 730;
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" + escape(value) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

</script>

</head>

BODY of page

Code:
<h1>Cookies test</h1><br>
 <input type="button" name="testcookieaccepted" value="Cookie" onclick="Set_Cookie();cookieExists();" />

FOOTER code in page
Code:
<script type="text/javascript">
	cookieName = "websitecookie";
	function cookieExists(cookieName) {

		if (!cookieName || cookieName == '') {
			return false;
		}
		var cookies = document.cookie.split(";");
		var cookieNames = new Array();
		for (x in cookies) {
			cookieNames.push(cookies[x].substr(0, cookies[x].indexOf("=")));
		}
		for (x in cookieNames) {
			if (cookieNames[x] == cookieName) {
				return true;
			}
		}
		return false;
	}

	if (cookieExists('websitecookie')) {

		//analytics code here

		var gaJsHost = (("https:" == document.location.protocol) ? "[URL unfurl="true"]https://ssl."[/URL] : "[URL unfurl="true"]http://www.");[/URL]
		document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

		var pageTracker = _gat._getTracker("**-******-1");
		pageTracker._setClientInfo(false);
		pageTracker._setAllowHash(false);
		pageTracker._setDetectFlash(false);
		pageTracker._setDetectTitle(false);
		pageTracker._trackPageview();



	}
</script>

On its own the google analytics javascript code looks like:

Code:
<script type="text/javascript">
	var gaJsHost = (("https:" == document.location.protocol) ? "[URL unfurl="true"]https://ssl."[/URL] : "[URL unfurl="true"]http://www.");[/URL]
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	</script>
	<script type="text/javascript">
		var pageTracker = _gat._getTracker("**-******-1");
		pageTracker._setClientInfo(false);
		pageTracker._setAllowHash(false);
		pageTracker._setDetectFlash(false);
		pageTracker._setDetectTitle(false);
		pageTracker._trackPageview();
</script>

Once again thanks for taking time out to look at this for me.
 
Hi

Sorry, for some reason I find your code hard to follow since the first post. This is the way I would implement it :
Code:
[red]<!DOCTYPE[/red] [maroon]html[/maroon][red]>[/red]
[b]<html>[/b]
[b]<head>[/b]
[b]<meta[/b] [maroon]charset[/maroon][teal]=[/teal][green][i]"ISO-8859-1"[/i][/green][b]>[/b]
[b]<title></title>[/b]
[b]<style>[/b]
div[teal]#nocookie[/teal] [teal]{[/teal]
  [COLOR=coral]background-color:[/color] [COLOR=darkgoldenrod]yellow[/color];
[teal]}[/teal]
div[teal]#yescookie[/teal] [teal]{[/teal]
  [COLOR=coral]background-color:[/color] [COLOR=darkgoldenrod]silver[/color];
[teal]}[/teal]
[b]</style>[/b]
[b]<script>[/b]
[b]function[/b] [COLOR=darkgoldenrod]enable[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] d[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal]
  d[teal].[/teal][COLOR=darkgoldenrod]setTime[/color][teal]([/teal]d[teal].[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()+[/teal][purple]1000[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]24[/purple][teal]*[/teal][purple]730[/purple][teal])[/teal]
  document[teal].[/teal]cookie[teal]=[/teal][green][i]'websitecookie=1; path=/; expires='[/i][/green][teal]+[/teal]d[teal].[/teal][COLOR=darkgoldenrod]toUTCString[/color][teal]()[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'nocookie'[/i][/green][teal]).[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]'none'[/i][/green]
  [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]}[/teal]
[b]function[/b] [COLOR=darkgoldenrod]disable[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] d[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal]
  d[teal].[/teal][COLOR=darkgoldenrod]setTime[/color][teal]([/teal]d[teal].[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()-[/teal][purple]1000[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]24[/purple][teal])[/teal]
  document[teal].[/teal]cookie[teal]=[/teal][green][i]'websitecookie=1; path=/; expires='[/i][/green][teal]+[/teal]d[teal].[/teal][COLOR=darkgoldenrod]toUTCString[/color][teal]()[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'yescookie'[/i][/green][teal]).[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]'none'[/i][/green]
[teal]}[/teal]
[b]function[/b] [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] gaJsHost [teal]=[/teal] [teal](([/teal][green][i]"https:"[/i][/green] [teal]==[/teal] document[teal].[/teal]location[teal].[/teal]protocol[teal])[/teal] [teal]?[/teal] [green][i]"[URL unfurl="true"]https://ssl."[/URL][/i][/green] [teal]:[/teal] [green][i]"[URL unfurl="true"]http://www."[/URL][/i][/green][teal]);[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal][COLOR=darkgoldenrod]unescape[/color][teal]([/teal][green][i]"%3Cscript src='"[/i][/green] [teal]+[/teal] gaJsHost [teal]+[/teal] [green][i]"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"[/i][/green][teal]));[/teal]
  [b]var[/b] pageTracker [teal]=[/teal] _gat[teal].[/teal][COLOR=darkgoldenrod]_getTracker[/color][teal]([/teal][green][i]"[b]-[/b]****-1"[/i][/green][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setClientInfo[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setAllowHash[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setDetectFlash[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setDetectTitle[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_trackPageview[/color][teal]();[/teal]
[teal]}[/teal]
[b]</script>[/b]
[b]</head>[/b]
[b]<body>[/b]
[b]<script>[/b]
[b]if[/b] [teal](![/teal]document[teal].[/teal]cookie[teal])[/teal] document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal][green][i]'<div id="nocookie">Cookies are currently disabled. If you want to use this site at full power, <a href="#" onclick="enable();return false">enable cookies</a>.</div>'[/i][/green][teal])[/teal]
[b]</script>[/b]
[b]<p>[/b]The document content comes here.[b]</p>[/b]
[b]<script>[/b]
[b]if[/b] [teal]([/teal]document[teal].[/teal]cookie[teal])[/teal] [teal]{[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal][green][i]'<div id="yescookie">Cookies are currently enabled. If you are concerned about privacy, you can <a href="#" onclick="disable();return false">disable cookies</a>.</div>'[/i][/green][teal])[/teal]
  [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]}[/teal]
[b]</script>[/b]
[b]</body>[/b]
[b]</html>[/b]
Note that I simplified the cookie check - if there is any cookie, it means they are enabled. This means, if other cookies will be set, you will have to unset them all in in the disable() function.

Feherke.
 
Thank You so much Feherke.

This is brilliant.

Just one thing, i don't think the cookies disable function is working as it should, when I click on the link to disable cookies, it doesn't remove the cookies when I go to look at them on firefox.

Or alternatively could it be set so that when the page loads, no cookies are set until the person clicks on enable cookies?

Once again thank you for your help and support, it is very appreciated
 
Hi

star171 said:
Just one thing, i don't think the cookies disable function is working as it should, when I click on the link to disable cookies, it doesn't remove the cookies when I go to look at them on firefox.
It works for me. As I mentioned below the code, I check for any cookie's existence. So if you set other cookies somewhere else, you have to clear them too in the disable() function.
star171 said:
Or alternatively could it be set so that when the page loads, no cookies are set until the person clicks on enable cookies?
That is how it currently works : only sets cookies if there are already cookies set.

Well, here is a modified version, this one strictly checks for the existence of the cookie with name websitecookie and tries[sup](*)[/sup] to delete them all when disabling :
Code:
[red]<!DOCTYPE[/red] [maroon]html[/maroon][red]>[/red]
[b]<html>[/b]
[b]<head>[/b]
[b]<meta[/b] [maroon]charset[/maroon][teal]=[/teal][green][i]"ISO-8859-1"[/i][/green][b]>[/b]
[b]<title></title>[/b]
[b]<style>[/b]
div[teal]#nocookie[/teal] [teal]{[/teal]
  [COLOR=coral]background-color:[/color] [COLOR=darkgoldenrod]yellow[/color];
[teal]}[/teal]
div[teal]#yescookie[/teal] [teal]{[/teal]
  [COLOR=coral]background-color:[/color] [COLOR=darkgoldenrod]silver[/color];
[teal]}[/teal]
[b]</style>[/b]
[b]<script>[/b]
[b]function[/b] [COLOR=darkgoldenrod]enable[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] d[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal]
  d[teal].[/teal][COLOR=darkgoldenrod]setTime[/color][teal]([/teal]d[teal].[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()+[/teal][purple]1000[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]24[/purple][teal]*[/teal][purple]730[/purple][teal])[/teal]
  document[teal].[/teal]cookie[teal]=[/teal][green][i]'websitecookie=1; path=/; expires='[/i][/green][teal]+[/teal]d[teal].[/teal][COLOR=darkgoldenrod]toUTCString[/color][teal]()[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'nocookie'[/i][/green][teal]).[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]'none'[/i][/green]
  [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]}[/teal]
[b]function[/b] [COLOR=darkgoldenrod]disable[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] d[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal]
  d[teal].[/teal][COLOR=darkgoldenrod]setTime[/color][teal]([/teal]d[teal].[/teal][COLOR=darkgoldenrod]getTime[/color][teal]()-[/teal][purple]1000[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]60[/purple][teal]*[/teal][purple]24[/purple][teal])[/teal]
  [highlight][b]var[/b] name[teal]=[/teal]document[teal].[/teal]cookie[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/\b\w+(?==)/g[/fuchsia][teal])[/teal][/highlight]
  [highlight][b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]name[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal][/highlight] document[teal].[/teal]cookie[teal]=[/teal][highlight]name[teal][[/teal]i[teal]]+[/teal][/highlight][green][i]'=1; path=/; expires='[/i][/green][teal]+[/teal]d[teal].[/teal][COLOR=darkgoldenrod]toUTCString[/color][teal]()[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'yescookie'[/i][/green][teal]).[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]'none'[/i][/green]
[teal]}[/teal]
[b]function[/b] [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] gaJsHost [teal]=[/teal] [teal](([/teal][green][i]"https:"[/i][/green] [teal]==[/teal] document[teal].[/teal]location[teal].[/teal]protocol[teal])[/teal] [teal]?[/teal] [green][i]"[URL unfurl="true"]https://ssl."[/URL][/i][/green] [teal]:[/teal] [green][i]"[URL unfurl="true"]http://www."[/URL][/i][/green][teal]);[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal][COLOR=darkgoldenrod]unescape[/color][teal]([/teal][green][i]"%3Cscript src='"[/i][/green] [teal]+[/teal] gaJsHost [teal]+[/teal] [green][i]"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"[/i][/green][teal]));[/teal]
  [b]var[/b] pageTracker [teal]=[/teal] _gat[teal].[/teal][COLOR=darkgoldenrod]_getTracker[/color][teal]([/teal][green][i]"[b]-[/b]****-1"[/i][/green][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setClientInfo[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setAllowHash[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setDetectFlash[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setDetectTitle[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_trackPageview[/color][teal]();[/teal]
[teal]}[/teal]
[b]</script>[/b]

[b]</head>[/b]
[b]<body>[/b]
[b]<script>[/b]
[b]if[/b] [teal](![/teal]document[teal].[/teal]cookie[highlight][teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/(?:^|;\s)websitecookie=/[/fuchsia][teal])[/teal][/highlight][teal])[/teal] document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal][green][i]'<div id="nocookie">Cookies are currently disabled. If you want to use this site at full power, <a href="#" onclick="enable();return false">enable cookies</a>.</div>'[/i][/green][teal])[/teal]
[b]</script>[/b]

[b]<p>[/b]The document content comes here.[b]</p>[/b]

[b]<script>[/b]
[b]if[/b] [teal]([/teal]document[teal].[/teal]cookie[highlight][teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/(?:^|;\s)websitecookie=/[/fuchsia][teal])[/teal][/highlight][teal])[/teal] [teal]{[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal][green][i]'<div id="yescookie">Cookies are currently enabled. If you are concerned about privacy, you can <a href="#" onclick="disable();return false">disable cookies</a>.</div>'[/i][/green][teal])[/teal]
  [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]}[/teal]
[b]</script>[/b]
[b]</body>[/b]
[b]</html>[/b]
[small](*) To delete a cookie you have to specify the properties ( domain, path and secure ), but those properties are not available for JavaScript.[/small]

I have no idea whether the European Union's directive allows it, but there is another alternative : Web Storage. More reliable, but otherwise also subject of controversy, however the concerns are technical, not security related. This should work more reliable :
Code:
[red]<!DOCTYPE[/red] [maroon]html[/maroon][red]>[/red]
[b]<html>[/b]
[b]<head>[/b]
[b]<meta[/b] [maroon]charset[/maroon][teal]=[/teal][green][i]"ISO-8859-1"[/i][/green][b]>[/b]
[b]<title></title>[/b]
[b]<style>[/b]
div[teal]#nocookie[/teal] [teal]{[/teal]
  [COLOR=coral]background-color:[/color] [COLOR=darkgoldenrod]yellow[/color];
[teal]}[/teal]
div[teal]#yescookie[/teal] [teal]{[/teal]
  [COLOR=coral]background-color:[/color] [COLOR=darkgoldenrod]silver[/color];
[teal]}[/teal]
[b]</style>[/b]
[b]<script>[/b]
[b]function[/b] [COLOR=darkgoldenrod]enable[/color][teal]()[/teal]
[teal]{[/teal]
  [highlight]localStorage[teal].[/teal][COLOR=darkgoldenrod]setItem[/color][teal]([/teal][green][i]'websitecookie'[/i][/green][teal],[/teal][green][i]'1'[/i][/green][teal])[/teal][/highlight]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'nocookie'[/i][/green][teal]).[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]'none'[/i][/green]
  [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]}[/teal]
[b]function[/b] [COLOR=darkgoldenrod]disable[/color][teal]()[/teal]
[teal]{[/teal]
  [highlight]localStorage[teal].[/teal][COLOR=darkgoldenrod]removeItem[/color][teal]([/teal][green][i]'websitecookie'[/i][/green][teal])[/teal][/highlight]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'yescookie'[/i][/green][teal]).[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]'none'[/i][/green]
[teal]}[/teal]
[b]function[/b] [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] gaJsHost [teal]=[/teal] [teal](([/teal][green][i]"https:"[/i][/green] [teal]==[/teal] document[teal].[/teal]location[teal].[/teal]protocol[teal])[/teal] [teal]?[/teal] [green][i]"[URL unfurl="true"]https://ssl."[/URL][/i][/green] [teal]:[/teal] [green][i]"[URL unfurl="true"]http://www."[/URL][/i][/green][teal]);[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]write[/color][teal]([/teal][COLOR=darkgoldenrod]unescape[/color][teal]([/teal][green][i]"%3Cscript src='"[/i][/green] [teal]+[/teal] gaJsHost [teal]+[/teal] [green][i]"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"[/i][/green][teal]));[/teal]
  [b]var[/b] pageTracker [teal]=[/teal] _gat[teal].[/teal][COLOR=darkgoldenrod]_getTracker[/color][teal]([/teal][green][i]"[b]-[/b]****-1"[/i][/green][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setClientInfo[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setAllowHash[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setDetectFlash[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_setDetectTitle[/color][teal]([/teal][b]false[/b][teal]);[/teal]
  pageTracker[teal].[/teal][COLOR=darkgoldenrod]_trackPageview[/color][teal]();[/teal]
[teal]}[/teal]
[b]</script>[/b]

[b]</head>[/b]
[b]<body>[/b]
[b]<script>[/b]
[b]if[/b] [teal](![/teal][highlight]localStorage[teal].[/teal][COLOR=darkgoldenrod]getItem[/color][teal]([/teal][green][i]'websitecookie'[/i][/green][teal])[/teal][/highlight][teal])[/teal] document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal][green][i]'<div id="nocookie">Cookies are currently disabled. If you want to use this site at full power, <a href="#" onclick="enable();return false">enable cookies</a>.</div>'[/i][/green][teal])[/teal]
[b]</script>[/b]

[b]<p>[/b]The document content comes here.[b]</p>[/b]

[b]<script>[/b]
[b]if[/b] [teal]([/teal][highlight]localStorage[teal].[/teal][COLOR=darkgoldenrod]getItem[/color][teal]([/teal][green][i]'websitecookie'[/i][/green][teal])[/teal][/highlight][teal])[/teal] [teal]{[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]writeln[/color][teal]([/teal][green][i]'<div id="yescookie">Cookies are currently enabled. If you are concerned about privacy, you can <a href="#" onclick="disable();return false">disable cookies</a>.</div>'[/i][/green][teal])[/teal]
  [COLOR=darkgoldenrod]gacookie[/color][teal]()[/teal]
[teal]}[/teal]
[b]</script>[/b]
[b]</body>[/b]
[b]</html>[/b]


Feherke.
 
Thanks Feherke,

Your right, there is no issue with your code, the issue is with the google analytics code.

Code:
_gat is not defined

var pageTracker = _gat._getTracker("**-******-1");

i'm having a look on google if there are any other people experiencing this problem.

Once again, thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top